简体   繁体   English

错误:没有唯一约束匹配给定键的引用表“bar”

[英]ERROR: there is no unique constraint matching given keys for referenced table "bar"

Trying to create this example table structure in Postgres 9.1:尝试在 Postgres 9.1 中创建这个示例表结构:

CREATE TABLE foo (
    name        VARCHAR(256) PRIMARY KEY
);

CREATE TABLE bar (
    pkey        SERIAL PRIMARY KEY,
    foo_fk      VARCHAR(256) NOT NULL REFERENCES foo(name), 
    name        VARCHAR(256) NOT NULL, 
    UNIQUE (foo_fk,name)
);

CREATE TABLE baz(   
    pkey        SERIAL PRIMARY KEY,
    bar_fk      VARCHAR(256) NOT NULL REFERENCES bar(name),
    name        VARCHAR(256)
);

Running the above code produces an error, which does not make sense to me:运行上面的代码会产生一个错误,这对我来说没有意义:

 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo" NOTICE: CREATE TABLE will create implicit sequence "bar_pkey_seq" for serial column "bar.pkey" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "bar_pkey" for table "bar" NOTICE: CREATE TABLE / UNIQUE will create implicit index "bar_foo_fk_name_key" for table "bar" NOTICE: CREATE TABLE will create implicit sequence "baz_pkey_seq" for serial column "baz.pkey" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "baz_pkey" for table "baz" ERROR: there is no unique constraint matching given keys for referenced table "bar"
********** Error **********

ERROR: there is no unique constraint matching given keys for referenced table "bar"
SQL state: 42830

Can anyone explain why this error arises?谁能解释为什么会出现这个错误?

It's because the name column on the bar table does not have the UNIQUE constraint.这是因为bar表上的name列没有UNIQUE约束。

So imagine you have 2 rows on the bar table that contain the name 'ams' and you insert a row on baz with 'ams' on bar_fk , which row on bar would it be referring since there are two rows matching?因此,假设您在bar表上有 2 行包含名称'ams'并且您在bar_fk上插入了带有'ams' baz行,因为有两行匹配,所以它会引用bar上的bar_fk行?

In postgresql all foreign keys must reference a unique key in the parent table, so in your bar table you must have a unique (name) index.在 postgresql 中,所有外键都必须引用父表中的唯一键,因此在bar表中必须有一个unique (name)索引。

See also http://www.postgresql.org/docs/9.1/static/ddl-constraints.html#DDL-CONSTRAINTS-FK and specifically:另请参阅http://www.postgresql.org/docs/9.1/static/ddl-constraints.html#DDL-CONSTRAINTS-FK ,特别是:

Finally, we should mention that a foreign key must reference columns that either are a primary key or form a unique constraint.最后,我们应该提到外键必须引用作为主键或形成唯一约束的列。

Emphasis mine.强调我的。

You should have name column as a unique constraint.您应该将名称列作为唯一约束。 here is a 3 lines of code to change your issues这是 3 行代码来更改您的问题

  1. First find out the primary key constraints by typing this code首先通过键入此代码找出主键约束

    \\d table_name

    you are shown like this at bottom "some_constraint" PRIMARY KEY, btree (column)您在底部"some_constraint" PRIMARY KEY, btree (column)

  2. Drop the constraint:删除约束:

     ALTER TABLE table_name DROP CONSTRAINT some_constraint
  3. Add a new primary key column with existing one:使用现有的主键列添加一个新的主键列:

     ALTER TABLE table_name ADD CONSTRAINT some_constraint PRIMARY KEY(COLUMN_NAME1,COLUMN_NAME2);

That's All.就这样。

when you do UNIQUE as a table level constraint as you have done then what your defining is a bit like a composite primary key see ddl constraints , here is an extract当您像所做的那样将UNIQUE作为表级约束执行时,您的定义有点像复合主键,请参阅ddl 约束,这里是摘录

This specifies that the combination of values in the indicated columns is unique across the whole table, though any one of the columns need not be (and ordinarily isn't) unique.这指定指示列中的值组合在整个表中是唯一的,尽管任何一列不需要(并且通常不是)唯一。

this means that either field could possibly have a non unique value provided the combination is unique and this does not match your foreign key constraint.这意味着如果组合是唯一的并且这与您的外键约束不匹配,则任一字段都可能具有非唯一值。

most likely you want the constraint to be at column level.很可能您希望约束处于列级别。 so rather then define them as table level constraints, 'append' UNIQUE to the end of the column definition like name VARCHAR(60) NOT NULL UNIQUE or specify indivdual table level constraints for each field.因此,与其将它们定义为表级约束,不如将UNIQUE '附加'到列定义的末尾,如name VARCHAR(60) NOT NULL UNIQUE或为每个字段指定单独的表级约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有唯一约束匹配给定引用表的键 - 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 远离错误:没有唯一约束匹配引用表的给定键 - 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 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table 如何修复错误“没有唯一约束匹配引用表的给定键” - How to fix error "there is no unique constraint matching given keys for referenced table" "错误:没有唯一约束匹配引用表“事件”的给定键" - error: there is no unique constraint matching given keys for referenced table "incident" 无法解释此错误:没有与引用表的给定键匹配的唯一约束 - Unable to explain this error: there is no unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM