我已经 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
通过问题列表,我引用了:
那么到底是什么导致了此错误?
最佳答案
documentation说:
在pipelines
表中,name
列和owner
列本身都不是唯一的。
我猜您实际上想在tasks
表中有一个两列的外键:
FOREIGN KEY(pipeline, owner) REFERENCES pipelines(name, owner)