我在 pgAdmin4 上的架构中有以下表定义,我正在尝试执行:
CREATE TABLE IF NOT EXISTS Reporting
(
SEQ integer,
Product character varying(30) NOT NULL,
Version integer NOT NULL,
Grade character varying(30) NOT NULL,
*Other_Detail...*,
CONSTRAINT pk_Reporting
PRIMARY KEY(SEQ, Product, Version, Grade),
CONSTRAINT fk_Reporting_ProductGrade
FOREIGN KEY(Product, Version, Grade)
REFERENCES Product_Grade(Product, Version, Grade)
)
该表应该引用另一个表:
CREATE TABLE IF NOT EXISTS Product_Grade
(
Product character varying(30) NOT NULL,
Version integer NOT NULL,
Grade character varying(30) NOT NULL,
*Other_Detail...*,
CONSTRAINT pk_ProductGrade
PRIMARY KEY(Product, Version, Grade, Sampling Point)
)
引用的表已标识其组合主键,并且子表具有外键约束,且组合主键的子集定义正确,对吗?
Postgres 是怎么抛出的:
错误:没有与引用表的给定键匹配的唯一约束
我也单独阐明了外键约束,但没有效果:
CONSTRAINT fk_Reporting_ProductGrade1
FOREIGN KEY(Product)
REFERENCES Product_Grade(Product),
CONSTRAINT fk_Reporting_ProductGrade2
FOREIGN KEY(Version)
REFERENCES Product_Grade(Version),
CONSTRAINT fk_Reporting_ProductGrade3
FOREIGN KEY(Grade)
REFERENCES Product_Grade(Grade)
编辑: 在
Product_Grade
表定义的PK约束中,我只有3个键作为组合PK,但表实际上有4个键:
CONSTRAINT pk_ProductGrade
PRIMARY KEY(Product, Version, Grade, **Sampling Point**)
(迟到的帖子,在我不久前得到答复后)
感谢@Atmo & @(Frank Heikens) 的评论指导我调整剧本。
我最终添加了一个带有我想要的 3 个字段的唯一约束:
CONSTRAINT unique_product_grade
UNIQUE (product, version, grade)
然后我可以将此组合指定为外键:
CONSTRAINT fk_reporting_productgrade
FOREIGN KEY(product, version, grade)
REFERENCES Product_Grade(product, version, grade)