添加外键失败,最后发现是主表键非唯一造成的:
create table if not exists papers(
id integer not NULL PRIMARY KEY auto_increment,
content_id varchar(18) not Null,
title varchar(288),
paper_url varchar(188),
memo varchar(288),
prov varchar(58),
b_has_imgs Bool,
b_has_table Bool,
b_has_attachments Bool)
#
其中第二个字段content_id为外键关联字段。
使用如下语句添加外键始终失败:
create table if not exists imgs_of_papers(
id integer not null Primary Key auto_increment,
contid varchar(18) not null,
foreign key(contid) references papers(content_id) on delete cascade on update cascade,
url varchar(188) not null)
网上搜了一些答案,众说纷纭。这个问题的产生可能有很多种,对于我这里,解决的办法很简单:
修改主表content_id字段:
content_id varchar(18) not Null UNIQUE,
在字段定义中添加UNIQUE,主表中相关字段需要唯一UNIQUE