2010-11-23 1322 views
1

你好 我有一個主表BASECOMPANYDATA BaseCompanyDataID作爲PK。這是由 2其他表格客戶& PRODUCTCOMPANIES。現在我有了我的聯繫人想要連接2個客戶,產品公司將有0或 更多的聯繫人。所以我在CONTACTS中創建了一個FK(BaseCompanyID),並連接到 BASECOMPANYDATA PK(BaseCompanyDataID)。但是,當我試圖插入 客戶端中存在的記錄時,出現以下錯誤: 錯誤:插入或更新表「xxxxx」違反外鍵約束「xxxxx」 細節:鍵(BaseCompanyDataID)=( 17)不存在於表「BaseCompanyData」中。 此ID存在於上述繼承的表(BaseCompanyData)中。 有人可以解釋爲什麼會發生這種情況?postgres錯誤:插入或更新表「xxxxx」違反外鍵約束「xxxxx」

在此先感謝

PS:嗯,我有4個表:

1.BASECOMPANYDATA與BaseCompanyDataID作爲PK和其他一些領域。 2.客戶從上面的表中繼承,因此它具有CustomerID作爲PK並且具有BASECOMPANYDATA表的字段,即BaseCompanyDataID等。 3.PRODUCTCOMPANIES從BASECOMPANYDATA繼承,因此它具有字段ProductCompanyID作爲PK和繼承的字段表格,如BaseCompanyDataID等 4.與ContactID作爲PK和BaseCompanyDataID作爲FK的聯繫。我試圖用兩種不同的方式連接桌面聯繫人。一個。 CONTACTS-> BaseCompanyID與CUSTOMERS-> BaseCompanyDataID和CONTACTS-> BaseCompanyID與PRODUCTCOMPANIES-> BaseCompanyDataID b。 CONTACTS-> BaseCompanyID與BASECOMPANYDATA-> BaseCompanyDataID結果是相同的錯誤。如果有的話,我可以使用繼承來創建FK。在此先感謝

+0

如果您在您的問題中添加了換行符,並且可能從您的表定義中選擇了一個片段,那就太好了。 – 2010-11-23 05:18:17

回答

3

您是否閱讀了繼承文檔?特別是5.8.1. Caveats部分?

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.

編輯:

繼承只有一半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) 
+0

這是我從SQL Server開始的,但我認爲使用PostgreSQL的繼承是很好的。似乎我會回到我在SQL Server中完成的工作。繼承有很多問題。 – ekekakos 2010-11-24 06:21:43

相關問題