2017-03-11 38 views
0

我有一個PostgreSQL數據庫,3個表和我的架構如下PostgreSQL中違反外鍵約束與傳承

--- Parent Table 

CREATE TABLE IF NOT EXISTS abc.parent(
    record_id   SERIAL PRIMARY KEY, 
    description  text NOT NULL 
); 

--- Child Table 


CREATE TABLE IF NOT EXISTS abc.child ( 
    total_count INT NOT NULL) INHERITS (abc.parent); 

-- Detail 

CREATE TABLE abc.detail(
    detail_id  int NOT NULL, 
    detail_description  text NOT NULL 
    record_id   int NOT NULL, 
    FOREIGN KEY (record_id) REFERENCES abc.parent(record_id) 
); 

然後我插入記錄雙方父母與子女表。

家長

|record_id|description| 
|1  |abcd  | 
|2  |efgh  | 

孩子

|record_id|description|total_count| 
|3  |xygh  |5   | 
|4  |mnop  |7   | 

當我嘗試插入記錄到以下兩個entires詳細信息表被成功

Detail 
|detail_id|detail_description|record_id| 
|100  |detail_desc1  | 1  | 
|200  |detail_desc2  | 2  | 

,但我不能插入帶有RECORD_ID 3項它給了我一個外鍵違規錯誤

有人可以解釋這個錯誤嗎?

我們可以建立在PostgreSQL這樣的外鍵關係與繼承

回答

1

不,這是行不通的。

the documentation說:

繼承特性的一個嚴重的侷限性是索引 (包括唯一約束)和外鍵約束只適用 單表,而不是他們的子女繼承。在外鍵約束的引用和引用兩邊都是如此, 。

「全局索引」是表繼承的重要缺失功能之一。