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