为什么以下内容会返回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)
);
我能做到......
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)
);
不会返回任何错误。但这意味着不同的猫主人不能给猫的名字两次(或更多)?
基本上,这就是我想要的:
cats.name | cats.owner
DAISY | BOB
NALA | BOB
NALA | CARL
答案 0 :(得分:1)
当我运行create时,出现以下错误:
架构创建失败:错误:列" id"在key中命名不存在:
您需要定义主键中使用的列。
使用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)
);