简体   繁体   English

没有唯一约束匹配给定引用表的键

[英]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.

相关问题 没有唯一约束与给定键匹配的具有多个主键的引用表 - No unique constraint matching given keys for referenced table with multiple primary keys sqlalchemy模式:没有唯一约束匹配给定表的键 - sqlalchemy schema: there is no unique constraint matching given keys for referenced table PSQL错误,没有唯一约束匹配给定表引用的键 - PSQL Error there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表 - ERROR: there is no unique constraint matching given keys for referenced table 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) 引用表“exam_subjects”的给定键没有唯一的约束匹配 - There is no unique constraint matching given keys for referenced table “ exam_subjects” 错误:没有唯一约束匹配给定键的引用表“bar” - ERROR: there is no unique constraint matching given keys for referenced table "bar" 远离错误:没有唯一约束匹配引用表的给定键 - Away around Error: There is no unique constraint matching given keys for referenced table Postgresql错误:没有唯一约束匹配给定键的引用表 - Postgresql ERROR: there is no unique constraint matching given keys for referenced table PostgreSQL错误:没有唯一约束匹配给定键的引用表 - PostgreSQL Error: there is not unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM