【发布时间】:2020-12-29 19:22:48
【问题描述】:
这是我拥有的两个表的匿名表示:
create table if not exists master_node (
book_name text primary key on conflict ignore not null
);
create table if not exists category_table (
book_name text not null,
category text not null,
foreign key(book_name) references master_node(book_name) on delete cascade,
unique(book_name, category) on conflict ignore
);
当我在表格中插入代码时:
insert into master_node
(book_name)
values
('Harry Potter'),
('Foundation'),
('The Catcher in the Rye')
和
insert or ignore into category_table
(book_name, category)
values
(Harry Potter', 'Fiction'),
('Harry Potter', 'Fantasy'),
('Foundation', 'Fiction'),
('Foundation', 'Science Fiction'),
('The Catcher in the Rye', 'Coming-of-age'),
('Moby Dick', 'Adventure')
我收到[SQLITE_CONSTRAINT] Abort due to constraint violation (FOREIGN KEY constraint failed) 错误,事务被回滚。
我希望通过使用insert or ignore 能够简单地跳过违反外键约束的行。我一直无法找到一种方法来获得这种行为。 sqlite 是否提供了这样做的方法?
【问题讨论】:
-
如果要违反外键约束有什么意义?如果要添加
master_node中不存在的记录,则不要删除约束。 -
insert or ignore仅忽略 UNIQUE 约束违规。 -
@Ivar 我希望 FK 约束阻止我添加违反约束的行。我只想要来自
master_node的具有book_name的行,但是如果我正在执行批量插入并且我有违反约束的行,我只想忽略这些行,而不是回滚整个插入。 -
@forpas 是的,我注意到它只适用于
unique和check,我只是在寻找相同的功能,但要使用外键约束。
标签: sqlite foreign-keys constraints exists constraintviolationexception