我正在嘗試設定帶有磁區的資料庫,但是在創建活動表時遇到了錯誤。3個表CREATE(沒有實際資料)如下:
CREATE TABLE collection (
id SERIAL PRIMARY KEY
);
CREATE TABLE asset (
id SERIAL,
collection_id INT,
CONSTRAINT fk_collection
FOREIGN KEY(collection_id)
REFERENCES collection(id),
CONSTRAINT asset_pkey PRIMARY KEY(id, collection_id)
)PARTITION BY LIST(collection_id);
CREATE TABLE activity(
id SERIAL,
collection_id INT,
CONSTRAINT fk_collection
FOREIGN KEY(collection_id)
REFERENCES collection(id),
CONSTRAINT activity_pkey PRIMARY KEY(id, collection_id),
asset_id INT,
CONSTRAINT fk_asset
FOREIGN KEY(asset_id)
REFERENCES asset(id)
) PARTITION BY LIST(collection_id);
ERROR: there is no unique constraint matching given keys for referenced table "asset"
SQL state: 42830
我對錯誤的理解是外鍵必須是唯一的。因為我必須將collection_id 作為主鍵添加到磁區項,所以我也不能將id 指定為唯一的。當我嘗試使 id 唯一時,會發生以下情況:
ALTER TABLE asset
ADD CONSTRAINT u_id UNIQUE (id);
ERROR: unique constraint on partitioned table must include all partitioning columns
DETAIL: UNIQUE constraint on table "asset" lacks column "collection_id" which is part of the partition key.
SQL state: 0A000
即使我成功地在 id 和 collection_id 上添加了 UNIQUE 約束,它仍然會繼續向我拋出 SQL 狀態:42830。我是否必須重組我的資料,或者有沒有更好的方法來解決這個問題,我沒有想到?
uj5u.com熱心網友回復:
將外鍵合并到一個復合鍵中是否足夠asset,或者這是否違反了資料的其他一些約束?
CREATE TABLE activity(
id SERIAL,
collection_id INT,
asset_id INT,
CONSTRAINT activity_pkey PRIMARY KEY(id, collection_id),
CONSTRAINT fk_asset_collection
FOREIGN KEY(asset_id, collection_id)
REFERENCES asset(id, collection_id)
) PARTITION BY LIST(collection_id);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/458881.html
標籤:sql PostgreSQL 数据库分区
上一篇:弧-資料建模
