【问题标题】:Foreign Key Constraint Failed in Sqlite3Sqlite3 中的外键约束失败
【发布时间】:2016-11-08 11:14:26
【问题描述】:

我第二次执行此脚本(这些表以前存在)并且收到外键约束失败错误。我是 Sqlite3 的初学者,无法找出背后的原因。

Schema.sql

PRAGMA foreign_keys = 1;

drop table if exists user;
create table user(uid integer primary key autoincrement, 
username text not null,
password text not null,
email text not null,
isadmin integer default 0);

drop table if exists asset;
create table asset(aid integer primary key autoincrement,
assetname text not null,
releasedate text,
owner integer default 0,
isreserved integer default 0,
foreign key(owner) references user(uid) on delete set default);

我正在用 Python 脚本读取这个文件。

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "main.py", line 37, in create_table
    conn.cursor().executescript(f.read())
sqlite3.IntegrityError: foreign key constraint failed

【问题讨论】:

  • 为我工作。您的表格是否包含数据?
  • 是的,这是导致错误的原因吗?

标签: python flask sqlite


【解决方案1】:

您将所有者(即文本)设置为外键。尝试将其设置为整数。

【讨论】:

  • 很奇怪,因为它可以在我的机器上运行。您是否尝试过再次构建数据库?
【解决方案2】:

删除表时,效果与删除所有行的效果相同,这些删除可能会失败。

首先删除所有表格,并按正确顺序(asset 首先)。

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 1970-01-01
    • 2018-09-11
    • 2013-05-29
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2013-01-20
    • 2016-08-17
    相关资源
    最近更新 更多