我在使用posgresql的项目中使用继承表。
类似的东西:
create table root
(
id bigserial,
some_data text,
...
);
create table leaf_a
(
data2 text
) inherits(root);
create table leaf_b
(
maybe_other_data text
) inherits(root);
到目前为止,一切都很顺利。
但是我最近添加了一个一对一关系的表,如果在leaf_a和leaf_b上使用,那么我就是这样创建的:
create table conf
(
id bigserial,
root_id bigint,
more_data text
);
到目前为止一切顺利,但现在我想创建一个约束:
alter table conf
add constraint plop foreign key (root_id) references root (id);
Postgres让我创建约束。
所以我添加一些数据:
insert into leaf_a (some_data, data2) values ('...', '...');
让我们说生成的id(来自根表的id)是42,我现在想要将数据添加到表conf:
insert into conf (root_id, more_data) values (42, '...');
这就是问题,postgres告诉我表根中没有id = 42的数据。
好的,那我怎么解决这个问题?
提前致谢。
答案 0 :(得分:0)
重新设计数据库模式(root有conf表的链接,leaf_a和leaf_b也是如此),添加3个约束(在root,leaf_a和leaf_b上)对我有用。