简体   繁体   English

PostgreSQL:复合主键:没有与给定键匹配的唯一约束

[英]PostgreSQL: Composite Primary Keys: no unique constraint matching given keys

Why does the following return there is no unique constraint matching given keys for referenced table "cats" ? 为什么以下返回的there is no unique constraint matching given keys for referenced table "cats"

CREATE TABLE cats (
  name varchar(36) NOT NULL,
  owner_id varchar(36) NOT NULL REFERENCES owners (id) ON DELETE CASCADE ON UPDATE CASCADE,
  description varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (name, owner_id)
);

I could do ... 我可以做 ...

CREATE TABLE cats (
  name varchar(36) NOT NULL,
  owner_id varchar(36) NOT NULL REFERENCES owners (id) ON DELETE CASCADE ON UPDATE CASCADE,
  description varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (name, owner_id),
  UNIQUE (name),
  UNIQUE (owner_id)
);

which doesn't return any error. 不会返回任何错误。 But this means that cat names can't be given twice (or more) by different cat owners? 但这意味着猫的主人不能给猫名字两次(或更多)吗?

Basically, this is what I want: 基本上,这就是我想要的:

cats.name | cats.owner  
DAISY     | BOB
NALA      | BOB
NALA      | CARL

When I run the create, I get the following error: 运行创建时,出现以下错误:

Schema Creation Failed: ERROR: column "id" named in key does not exist: 架构创建失败:错误:键中命名的列“ id”不存在:

You need to define the columns used in the primary key. 您需要定义主键中使用的列。

Using name does seem to fix the problem: 使用name似乎可以解决问题:

CREATE TABLE cats (
  name varchar(36) NOT NULL,
  owner_id varchar(36) NOT NULL REFERENCES owners (id) ON DELETE CASCADE ON UPDATE CASCADE,
  description varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (name, owner_id)
);

暂无
暂无

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

相关问题 没有唯一约束与给定键匹配的具有多个主键的引用表 - No unique constraint matching given keys for referenced table with multiple primary keys SQL-尽管存在主键,但“没有唯一约束匹配给定键” - SQL - “No Unique Constraint Matching Given Keys” Despite Presence of Primary Key 给定键没有唯一约束匹配 - No unique constraint matching given keys 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) 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 没有唯一约束匹配给定引用表的键 - No unique constraint matching given keys for referenced table 数据库原型 Postgresql:错误:没有唯一约束匹配给定键的引用表“消息” - Database prototype Postgresql: ERROR: there is no unique constraint matching given keys for referenced table “messages” 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM