admin管理员组文章数量:1401598
I research declarative partitioning and prepared such query for partitioned table:
create table _my_message
(
id uuid not null,
chat_id uuid constraint _my_message_chat_fkey references _chat,
...
CONSTRAINT _my_message_id_chat_id_pk PRIMARY KEY (id, chat_id)
) PARTITION BY HASH (chat_id);
And I have another table where I have to have reference to id
column of _my_message
CREATE TABLE _document
(
id uuid NOT NULL CONSTRAINT _documents_pkey PRIMARY KEY,
chat_id uuid NOT NULL,
message_id uuid CONSTRAINT _documents_message_pkey REFERENCES _my_message (id)
);
But now I have an error because of lack of unique constraint for id
in _my_message
SQL State : 42830
Error Code : 0
Message : ERROR: there is no unique constraint matching given keys for referenced table "_partitioned_message"
And I'm really confused because it's not allowed to have more unique constraints for partitioned table.
If I try to use construction with id uuid not null primary key
I'll get exception [42P16] ERROR: multiple primary keys for table "_my_message" are not allowed
.
If I try to use id uuid not null unique
I'll get exception [0A000] ERROR: unique constraint on partitioned table must include all partitioning columns
I research declarative partitioning and prepared such query for partitioned table:
create table _my_message
(
id uuid not null,
chat_id uuid constraint _my_message_chat_fkey references _chat,
...
CONSTRAINT _my_message_id_chat_id_pk PRIMARY KEY (id, chat_id)
) PARTITION BY HASH (chat_id);
And I have another table where I have to have reference to id
column of _my_message
CREATE TABLE _document
(
id uuid NOT NULL CONSTRAINT _documents_pkey PRIMARY KEY,
chat_id uuid NOT NULL,
message_id uuid CONSTRAINT _documents_message_pkey REFERENCES _my_message (id)
);
But now I have an error because of lack of unique constraint for id
in _my_message
SQL State : 42830
Error Code : 0
Message : ERROR: there is no unique constraint matching given keys for referenced table "_partitioned_message"
And I'm really confused because it's not allowed to have more unique constraints for partitioned table.
If I try to use construction with id uuid not null primary key
I'll get exception [42P16] ERROR: multiple primary keys for table "_my_message" are not allowed
.
If I try to use id uuid not null unique
I'll get exception [0A000] ERROR: unique constraint on partitioned table must include all partitioning columns
- I would get rid of the foreign key and the primary key on the partitioned table. Use primary keys on the individual partitions instead. If you can partition the referencing table appropriately, that would allow you to define foreign key constraints between the individual partitions. – Laurenz Albe Commented Mar 24 at 6:51
1 Answer
Reset to default 3You use two columns for identifying your _my_message
rows (in its primary key). You need to use a multi-column foreign key constraint as well then to reference them, which seems easily possible since your _document
rows appear to belong to the same chat_id
:
CREATE TABLE _document (
id uuid NOT NULL PRIMARY KEY,
chat_id uuid NOT NULL,
message_id uuid,
CONSTRAINT _documents_message FOREIGN KEY (chat_id, message_id) REFERENCES _my_message(chat_id, id)
);
本文标签: partitioningHow to use references to partitioned table in PostgreSQLStack Overflow
版权声明:本文标题:partitioning - How to use references to partitioned table in PostgreSQL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744265843a2597942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论