postgres 错误:在表“xxxxx”上插入或更新违反外键约束“xxxxx”

发布于 2024-10-04 06:15:52 字数 1004 浏览 1 评论 0原文

你好 我有一个主表 BASECOMPANYDATA ,其中 BaseCompanyDataID 作为 PK。这是继承自 2 个其他表 CUSTOMERS &产品公司。现在我有表 CONTACTS 想要与另外 2 个人建立联系,因为客户和产品公司将有 0 或 更多的接触。所以我在CONTACTS中做了一个FK(BaseCompanyID)并连接到 BASECOMPANYDATA PK(BaseCompanyDataID)。但是当我尝试插入联系人时 客户中存在的记录我收到以下错误: 错误:表“xxxxx”的插入或更新违反了外键约束“xxxxx” 详细信息:表“BaseCompanyData”中不存在键 (BaseCompanyDataID)=(17)。 该ID存在于上面继承的表(BaseCompanyData)中。 有人可以解释为什么会发生这种情况吗?

预先感谢

PS:嗯,我有 4 个表:

1.BASECOMPANYDATA,其中 BaseCompanyDataID 作为 PK 和一些其他字段。 2.CUSTOMERS继承自上表,因此它的CustomerID为PK,并具有BASECOMPANYDATA表的字段,即BaseCompanyDataID等。 3.ProductCOMPANIES继承自BASECOMPANYDATA,因此它具有ProductCompanyID字段作为PK以及继承表的字段,例如BaseCompanyDataID等。 4.CONTACTS,ContactID 作为 PK,BaseCompanyDataID 作为 FK。我尝试用两种不同的方式连接表 CONTACTS。一个。 CONTACTS->BaseCompanyID 与 CUSTOMERS->BaseCompanyDataID 以及 CONTACTS->BaseCompanyID 与 ProductCOMPANIES->BaseCompanyDataID b. CONTACTS->BaseCompanyID 和 BASECOMPANYDATA->BaseCompanyDataID 结果是相同的错误。关于如何使用继承创建 FK 的任何答案(如果有)。提前致谢

Hello
I have a main table BASECOMPANYDATA with BaseCompanyDataID as a PK. This is inhereted by
2 other tables CUSTOMERS & PRODUCTCOMPANIES. Now I have the table CONTACTS which I
want to connect with the 2 others as the customers and the productcompanies will have 0 or
more contacts. So I made a FK(BaseCompanyID) in CONTACTS and connected to the
BASECOMPANYDATA PK(BaseCompanyDataID). But when I am trying to insert a contact for
a record which exists in CUSTOMERS I get the following error:
ERROR: insert or update on table "xxxxx" violates foreign key contrain "xxxxx"
DETAIL: Key (BaseCompanyDataID)=(17) is not present in table "BaseCompanyData".
This ID exists in the above inherited table (BaseCompanyData).
Can someone explain why is this happening?

Thanks in advance

PS:Well, I have 4 tables:

1.BASECOMPANYDATA with BaseCompanyDataID as PK and some other fields.
2.CUSTOMERS which inherits from the above table so it has CustomerID as PK and has the fields of the BASECOMPANYDATA table namely BaseCompanyDataID etc.
3.PRODUCTCOMPANIES which inherits from BASECOMPANYDATA so it has the fields ProductCompanyID as PK and the fields of the inherited table like BaseCompanyDataID etc.
4.CONTACTS with ContactID as PK and BaseCompanyDataID as a FK. I tried to connect the table CONTACTS with 2 different ways. a. CONTACTS->BaseCompanyID with CUSTOMERS->BaseCompanyDataID and CONTACTS->BaseCompanyID with PRODUCTCOMPANIES->BaseCompanyDataID b. CONTACTS->BaseCompanyID with BASECOMPANYDATA->BaseCompanyDataID The result was the same error. Any answer on how I can create the FK using the inheritance, if there is. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2024-10-11 06:15:52

您阅读了继承文档吗?特别是5.8.1。注意事项部分?

http://www.postgresql.org/docs/9.0/static/ddl -inherit.html

...

类似地,如果我们指定 cars.name 引用其他表,则此约束不会自动传播到大写字母。在这种情况下,您可以通过手动向大写字母添加相同的 REFERENCES 约束来解决此问题。

编辑:

继承在 Postgsresql 中仅实现了一半。如果您想节省输入,请在创建表中查看 like

在您的第一个问题中,我看到有人推荐的内容与我所说的完全相同。现在你有问题吗?嗯...

这是我从你的转发中得到的伪sql:

base
    baseid

customers(base)
    baseid
    id

products(base)
    baseid
    id

contacts
    id
    baseid references base(baseid)

用老式的方式来做吧!

base
    id

customers
    base_id references base(id)
    id

products(base)
    base_id references base(id)
    id

contacts
    id
    base_id references base(id)

Did you read through the inheritance docs? Especially the 5.8.1. Caveats section?

http://www.postgresql.org/docs/9.0/static/ddl-inherit.html

...

Similarly, if we were to specify that cities.name REFERENCES some other table, this constraint would not automatically propagate to capitals. In this case you could work around it by manually adding the same REFERENCES constraint to capitals.

edit:

Inheritance is only half implemented in Postgsresql. If you want to save typing check out like in create table

In your first question I see the person recommended exactly the same thing I said. And now you have a problem? Hmm ...

This is pseudo sql I get from your repost:

base
    baseid

customers(base)
    baseid
    id

products(base)
    baseid
    id

contacts
    id
    baseid references base(baseid)

Just do it the good old fashioned way!

base
    id

customers
    base_id references base(id)
    id

products(base)
    base_id references base(id)
    id

contacts
    id
    base_id references base(id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文