postgres 错误:在表“xxxxx”上插入或更新违反外键约束“xxxxx”
你好 我有一个主表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您阅读了继承文档吗?特别是
5.8.1。注意事项
部分?http://www.postgresql.org/docs/9.0/static/ddl -inherit.html
...
编辑:
继承在 Postgsresql 中仅实现了一半。如果您想节省输入,请在创建表中查看
like
在您的第一个问题中,我看到有人推荐的内容与我所说的完全相同。现在你有问题吗?嗯...
这是我从你的转发中得到的伪sql:
用老式的方式来做吧!
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
...
edit:
Inheritance is only half implemented in Postgsresql. If you want to save typing check out
like
in create tableIn 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:
Just do it the good old fashioned way!