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

标签 sqlite foreign-keys

我已经 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说:

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 表中,nameowner 列本身都不是唯一的。

我猜你实际上想在 tasks 表中有一个两列的外键:

FOREIGN KEY(pipeline, owner) REFERENCES pipelines(name, owner)

关于sqlite - 是什么导致这个 sqlite 外键不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915331/

相关文章:

mysql - 无法向现有表添加外键

java - if 语句不起作用

nhibernate - SQLite 与 NHibernate 结合是否支持参照完整性/外键?

c# - 使用 C# 查询 SQlite 数据库时出现 ConstraintException

foreign-keys - 水线关联,更改外键?

mysql - 同一表和组的多个外键

mysql - 最快的 SQL 全文搜索

android.database.sqlite.SQLiteException : near "ORDER": syntax error (code 1): ,

mysql查询返回引用2个不同外键的相同字段

mysql - 限制数据库级别的关联记录数