0

我在使用 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 的数据。

好的,那么我该如何解决这个问题?

提前致谢。

4

1 回答 1

0

重新设计数据库模式(root 有一个 conf 表的链接,leaf_a 和 leaf_b 也是如此)并添加 3 个约束(在 root、leaf_a 和 leaf_b 上)对我有用。

于 2013-10-04T14:30:28.480 回答