我已经 checkout this question ,并认为我有答案 - 但后来我觉得不对。
我有以下精简示例:
CREATE TABLE pipelines (
name VARCHAR NOT NULL,
owner VARCHAR NOT NULL,
description VARCHAR,
PRIMARY KEY (name, owner),
FOREIGN KEY(owner) REFERENCES user (id)
);
CREATE TABLE tasks (
id INTEGER NOT NULL,
title VARCHAR,
pipeline VARCHAR,
owner VARCHAR,
PRIMARY KEY (id),
FOREIGN KEY(pipeline) REFERENCES pipelines (name),
FOREIGN KEY(owner) REFERENCES pipelines (owner)
);
CREATE TABLE user (
id VARCHAR NOT NULL,
name VARCHAR,
password VARCHAR,
PRIMARY KEY (id)
);
pragma foreign_keys=on;
insert into user values ('wayne', '', '');
insert into pipelines values ('pipey', 'wayne', '');
insert into tasks values (1, 'hello', 'pipey', 'wayne');
当执行这段代码时,它跳出:
$ sqlite3 foo.sq3 '.read mismatch.sql'
Error: near line 27: foreign key mismatch
通过我引用的问题中的列表:
- 父表(用户)存在。
- 存在父列(名称、所有者)
- 实际上,父列是主键(我认为可能是原来的主键)
- 子表引用父表中的所有主键列
那么究竟是什么导致了这个错误?
最佳答案
Usually, the parent key of a foreign key constraint is the primary key of the parent table. If they are not the primary key, then the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.
在 pipelines
表中,name
和 owner
列本身都不是唯一的。
我猜你实际上想在 tasks
表中有一个两列的外键:
FOREIGN KEY(pipeline, owner) REFERENCES pipelines(name, owner)
关于sqlite - 是什么导致这个 sqlite 外键不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915331/