我已经检查了
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说:
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.
在管道表中,名称和所有者列本身都不是唯一的.
我猜你真的想在tasks表中有一个两列的外键:
FOREIGN KEY(pipeline,owner) REFERENCES pipelines(name,owner)