首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PostgreSQL声明式分区-对分区表的唯一约束必须包括所有分区列

PostgreSQL声明式分区-对分区表的唯一约束必须包括所有分区列
EN

Stack Overflow用户
提问于 2021-12-29 23:07:25
回答 1查看 487关注 0票数 1

我试图创建一个引用自身的分区表,创建一个双链接列表。

代码语言:javascript
复制
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);

添加主键后,将得到以下错误。

代码语言:javascript
复制
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,它就能工作了。

代码语言:javascript
复制
alter table test2 add primary key (id, category);

但是,将FK添加到自身中是行不通的。

代码语言:javascript
复制
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/

EN

回答 1

Stack Overflow用户

发布于 2021-12-30 13:08:22

似乎没有直接的解决办法。PostgreSQL与v14一样,根本不支持这一点。一种解决方案是使用触发器来执行“外键”行为。另一种是使用多列外键.两者都远非最佳。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70525917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档