我试图创建一个引用自身的分区表,创建一个双链接列表。
CREATE TABLE test2 (
id serial NOT NULL,
category integer NOT NULL,
time timestamp(6) NOT NULL,
prev_event integer,
next_event integer
) PARTITION BY HASH (category);添加主键后,将得到以下错误。
alter table test2 add primary key (id);
ERROR: unique constraint on partitioned table must include all partitioning columns
DETAIL: PRIMARY KEY constraint on table "test2" lacks column "category" which is part of the partition key.为什么唯一的约束要求包括所有分区列?编辑:现在我明白为什么需要这样做了:https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-LIMITATIONS
一旦我在两列中添加PK,它就能工作了。
alter table test2 add primary key (id, category);但是,将FK添加到自身中是行不通的。
alter table test2 add foreign key (prev_event) references test2 (id) on update cascade on delete cascade;
ERROR: there is no unique constraint matching given keys for referenced table "test2"因为PK不仅是id,而且是id类别,所以我不能创建指向id的FK。
有没有办法处理这件事,还是我遗漏了什么?
如果可能的话,我想避免使用继承分区。
EDIT2:看来这是一个已知的问题。https://www.reddit.com/r/PostgreSQL/comments/di5mbr/postgresql_12_foreign_keys_and_partitioned_tables/f3tsoop/
发布于 2021-12-30 13:08:22
似乎没有直接的解决办法。PostgreSQL与v14一样,根本不支持这一点。一种解决方案是使用触发器来执行“外键”行为。另一种是使用多列外键.两者都远非最佳。
https://stackoverflow.com/questions/70525917
复制相似问题