我有一个date_dimension
表定义:
CREATE TABLE date_dimension
(
id integer primary key,
date text,
year double precision,
year_for_week double precision,
quarter double precision
);
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个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)
);
Run Code Online (Sandbox Code Playgroud)
错误是:
ERROR: there is no unique constraint matching given keys for referenced
table "date_dimension"
SQL state: 42830
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题.
Fli*_*mzy 15
该错误告诉您问题:您没有date_dimension
与您的外键约束匹配的唯一约束.
但是,这会导致更大的设计问题:您的外键关系没有任何意义.
您可以用以下方法解决您的"问题":
CREATE UNIQUE INDEX date_dimension(id,id);
Run Code Online (Sandbox Code Playgroud)
但那是愚蠢的,因为id
总是一样的.它也可以表示为:
FOREIGN KEY (evaluation_date) REFERENCES date_dimension(id);
Run Code Online (Sandbox Code Playgroud)
然后摆脱effective_date
列,这将始终与evaluation_date
您的示例相同.
或者......你可能真的想要两个FK关系:
FOREIGN KEY (evaluation_date) REFERENCES date_dimension(id);
FOREIGN KEY (effective_date) REFERENCES date_dimension(id);
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 10
我想你正在寻找两个独立的外键:
foreign key (evaluation_date) references date_dimension(id),
foreign key (effective_date) references date_dimension(id)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29439 次 |
最近记录: |