加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

是什么导致这个sqlite外键不匹配?

发布时间:2020-12-12 23:40:52 所属栏目:百科 来源:网络整理
导读:我已经检查了 this question,并且认为我得到了答案 – 但那时我看起来并不合适. 我有以下简化示例: CREATE TABLE pipelines ( name VARCHAR NOT NULL,owner VARCHAR NOT NULL,description VARCHAR,PRIMARY KEY (name,owner),FOREIGN KEY(owner) REFERENCES
我已经检查了 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读