那里!我在 sqlite3 中有一个失败的外键约束,我真的不知道为什么。我正在使用此处描述的复合外键(http://www.sqlite.org/foreignkeys.html#fk_composite)和“普通”外键。
这是我的模式:
sqlite> .schema sources
CREATE TABLE sources(
source_id VARCHAR(16) NOT NULL,
source_type INTEGER NOT NULL,
title VARCHAR(128) NOT NULL,
year INTEGER,
month INTEGER,
PRIMARY KEY(source_id),
UNIQUE(title)
);
sqlite> .schema author_aliases
CREATE TABLE author_aliases(
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
forenames VARCHAR(128),
surname VARCHAR(128) NOT NULL,
PRIMARY KEY(author_id, alias_id),
FOREIGN KEY(author_id) REFERENCES authors(author_id),
UNIQUE(forenames, surname)
);
sqlite> .schema alias_source_relations
CREATE TABLE alias_source_relations(
source_id VARCHAR(16) NOT NULL,
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
PRIMARY KEY(source_id, author_id, alias_id),
FOREIGN KEY(source_id) REFERENCES sources(source_id),
FOREIGN KEY(author_id, alias_id) REFERENCES author_aliases(author_id, alias_id)
);
这是外键所指的数据:
sqlite> SELECT * FROM sources WHERE source_id='Allen1980';
Allen1980|0|The definition of electronegativity and the chemistry of the noble gases|1980|
sqlite> SELECT * FROM author_aliases WHERE author_id=1 and alias_id=1;
1|1|Leland C.|Allen
sqlite> SELECT * FROM authors WHERE author_id=1;
1|Leland Cullen|Allen
这是我的插入:
sqlite> INSERT INTO alias_source_relations VALUES(1, 1, 'Allen1980');
Error: foreign key constraint failed
有谁知道我错过了什么?谢谢你的帮助!
问候,玛丽安