【发布时间】:2017-03-11 04:37:51
【问题描述】:
我有一个 PostgreSQL 数据库、3 个表和我的架构如下
--- 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 |
当我尝试将记录插入到两个完整之后的详细表中时成功
Detail
|detail_id|detail_description|record_id|
|100 |detail_desc1 | 1 |
|200 |detail_desc2 | 2 |
但我无法插入带有 record_id 3 的条目,它给了我一个外键违规错误
谁能解释一下这个错误??
我们可以在Postgresql中用继承创建这样的外键关系
【问题讨论】:
标签: sql postgresql foreign-keys