简体   繁体   English

为什么在标识列上出现外键不唯一错误?

[英]Why do I get a foreign key not unique error on an identity column?

If I try to create two tables like this in PostgreSQL 14.3:如果我尝试在 PostgreSQL 14.3 中创建两个这样的表:

CREATE TABLE IF NOT EXISTS foo (
  id INT4 GENERATED BY DEFAULT AS IDENTITY (INCREMENT BY 1 MINVALUE -2147483648 START -2147483648),
  foo TEXT UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS bar (
  id INT4 GENERATED BY DEFAULT AS IDENTITY (INCREMENT BY 1 MINVALUE -2147483648 START -2147483648),
  foo_id INT4 NOT NULL REFERENCES foo(id)
);

I get this output:我得到这个 output:

$ psql test -f bug01.sql
CREATE TABLE
psql:bug01.sql:9: ERROR:  there is no unique constraint matching given keys for referenced table "foo"

I thought GENERATED BY DEFAULT AS IDENTITY would have satisfied the uniqueness constraint.我认为GENERATED BY DEFAULT AS IDENTITY会满足唯一性约束。 What am I missing?我错过了什么? And how do I resolve the issue?我该如何解决这个问题?


UPDATE: Applying Bergi's answer and comments, I added a primary key constraint to each identity column and that solved the problem:更新:应用 Bergi 的回答和评论,我向每个标识列添加了一个主键约束并解决了问题:

CREATE TABLE IF NOT EXISTS foo (
  id INT4 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (INCREMENT BY 1 MINVALUE -2147483648 START -2147483648),
  foo TEXT UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS bar (
  id INT4 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (INCREMENT BY 1 MINVALUE -2147483648 START -2147483648),
  foo_id INT4 NOT NULL REFERENCES foo(id)
);
$ psql test -f bug01.sql
CREATE TABLE
CREATE TABLE

Thanks!谢谢!

Well, no.好吧,不。GENERATED just means it's harder to overwrite (requiring special syntax) than a DEFAULT value, and AS IDENTITY is just a fancy way of defining an implicit sequence, similar to SERIAL .GENERATED只是意味着它比DEFAULT值更难覆盖(需要特殊语法),而AS IDENTITY只是一种定义隐式序列的奇特方式,类似于SERIAL

You still can insert duplicates in your column, either by resetting the sequence or using OVERRIDING SYSTEM VALUE , you must declare the appropriate constraints to prevent this.您仍然可以通过重置序列或使用OVERRIDING SYSTEM VALUE在您的列中插入重复项,您必须声明适当的约束以防止这种情况发生。 Simply make your id the PRIMARY KEY of the table.只需将您的id设为表的PRIMARY KEY

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

相关问题 SQL,如何添加需要作为主键、唯一且自动递增的列? 错误表示“IDENTITY”处或附近的语法错误 - SQL, How do I add a column which needs to be a primary key, unique and auto-incrementing? Error says syntax error at or near "IDENTITY" 为什么我在不存在的外键上得到SQL state:23503 - Why do I get SQL state:23503 on an inexistent foreign key Postgres - 非唯一列上的外键 - Postgres - foreign key on not unique column 如何获取外键引用主表的表的表名和列名? - How do i get table name and column name of the table that has foreign key referencing to master table? 如何将现有列转换为外键? - How do I convert an existing column to Foreign Key? 如何确保具有相同外键的所有行在我的 PostgreSQL 数据库中具有唯一名称? - How do I make sure that all rows with the same foreign key have unique names in my PostgreSQL database? 如何基于Rails中的外键列选择唯一记录? - How to select unique records based on foreign key column in Rails? 使先前存在的外键列在 postgres 中具有唯一约束 - Make a previously existing foreign key column have a unique constraint in postgres 带外键的唯一约束 - Unique constraint with foreign key 如何将列更新为外键? - How do you update a column to a foreign key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM