[英]No unique constraint matching given keys for referenced table
I have a date_dimension
table definition: 我有一个date_dimension
表定义:
CREATE TABLE date_dimension
(
id integer primary key,
date text,
year double precision,
year_for_week double precision,
quarter double precision
);
I am trying to create a fact
table that fails 我试图创建一个失败的fact
表
create table fact (
id serial primary key,
contract integer,
component integer,
evaluation_date integer,
effective_date integer,
foreign key (evaluation_date, effective_date) references date_dimension(id, id)
);
The error is : 错误是:
ERROR: there is no unique constraint matching given keys for referenced
table "date_dimension"
SQL state: 42830
I am not sure how to fix this. 我不知道如何解决这个问题。
The error tells you the problem: You don't have a unique constraint on date_dimension
that matches your foreign key constraint. 该错误告诉您问题:您在date_dimension
没有与外键约束匹配的唯一约束。
However, this leads to the bigger design problem: Your foreign key relationship doesn't make any sense. 但是,这会导致更大的设计问题:您的外键关系没有任何意义。
You could possibly solve your "problem" with: 您可以用以下方法解决您的“问题”:
CREATE UNIQUE INDEX date_dimension(id,id);
But that's dumb, because id
is always the same. 但那是愚蠢的,因为id
总是一样的。 It could also be expressed as: 它也可以表示为:
FOREIGN KEY (evaluation_date) REFERENCES date_dimension(id);
Then getting rid of the effective_date
column, which would always be identical to evaluation_date
in your example. 然后删除effective_date
列,它在您的示例中始终与evaluation_date
相同。
Or... you probably really want two FK relationships: 或者......你可能真的想要两个FK关系:
FOREIGN KEY (evaluation_date) REFERENCES date_dimension(id);
FOREIGN KEY (effective_date) REFERENCES date_dimension(id);
I think you are looking for two separate foreign keys: 我想你正在寻找两个独立的外键:
foreign key (evaluation_date) references date_dimension(id),
foreign key (effective_date) references date_dimension(id)
Don't you just want to create two separate foreign key references to the date dimension as follows: 您是否只想创建两个对日期维度的单独外键引用,如下所示:
create table fact (
id serial primary key,
contract integer,
component integer,
evaluation_date integer,
effective_date integer,
foreign key (evaluation_date) references date_dimension(id),
foreign key (effective_date) references date_dimension(id)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.