Way*_*ner 8 sqlite foreign-keys
我已经检查了这个问题,并且认为我有答案 - 但那时我看起来并不合适.
我有以下简化示例:
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');
Run Code Online (Sandbox Code Playgroud)
执行此代码时,它会失败:
$ sqlite3 foo.sq3 '.read mismatch.sql'
Error: near line 27: foreign key mismatch
Run Code Online (Sandbox Code Playgroud)
通过我引用的问题列表:
那么世界上可能会导致这个错误呢?
CL.*_*CL. 13
该文件说:
通常,外键约束的父键是父表的主键.如果它们不是主键,则父键列必须共同使用UNIQUE约束或具有UNIQUE索引.
在pipelines表中,列name和owner列本身都不是唯一的.
我猜你真的想在tasks表中有一个两列的外键:
FOREIGN KEY(pipeline, owner) REFERENCES pipelines(name, owner)
Run Code Online (Sandbox Code Playgroud)