FOREIGN KEY 约束失败 sqlite python

问题描述 投票:0回答:1

嗨,我刚刚出现了这个 sql 错误(之前工作正常),我对此感到非常困惑,我找不到任何可行的调整。 我在下面分享数据框的架构和代码 如果有人可以帮助我,我很感激

CREATE TABLE users(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT NOT NULL,
hash TEXT NOT NULL);

CREATE TABLE sqlite_sequence(name,seq);

CREATE TABLE comments(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
user_id INTEGER NOT NULL,
comment TEXT NOT NULL,
url TEXT NOT NULL,
data DATETIME NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id));

CREATE TABLE video(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
user_id INTEGER NOT NULL,
video_id INTEGER NOT NULL,
data DATETIME NOT NULL,
url TEXT NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id));

CREATE TABLE video_comment(
video_id INTEGER,
comment_id INTEGER,
FOREIGN KEY(video_id) REFERENCES video(id),
FOREIGN KEY(comment_id) REFERENCES comments(id));

代码:

            for elemen in comments:
                print(elemen.text)
                db.execute("INSERT INTO comments (user_id, comment,data,url) VALUES (?,?,?,?)", 1, elemen.text,current_time, url)
                comment_id = db.execute("SELECT id FROM comments WHERE comment = ?", elemen.text)[0]
                print(comment_id)
                print(comment_id['id'])

#error appear now 
                db.execute("INSERT INTO video_comment (video_id,comment_id) VALUES (1,?)", int(comment_id['id']))

            db.execute("INSERT INTO video (user_id,video_id,data,url) VALUES (?,?,?,?)", 1,1, current_time, url)

comments 是评论列表。 错误出现在两次print()之后,代码可以将注释放入数据库并且崩溃后。

谢谢!!

如果我使用带有 sqlite3 的终端,相同的查询工作正常,所以我不明白。

错误=

Traceback (most recent call last):
  File "/Users/pietrocaracristi/PycharmProjects/pythonProject-1-12/final-project/main.py", line 184, in scrape_comments
    db.execute("INSERT INTO video_comment (video_id,comment_id) VALUES (1,?)", int(comment_id['id']))
  File "/Users/pietrocaracristi/PycharmProjects/pythonProject-1-12/final-project/venv/lib/python3.10/site-packages/cs50/sql.py", line 29, in decorator
    return f(*args, **kwargs)
  File "/Users/pietrocaracristi/PycharmProjects/pythonProject-1-12/final-project/venv/lib/python3.10/site-packages/cs50/sql.py", line 405, in execute
    raise e
ValueError: FOREIGN KEY constraint failed
python sql sqlite
1个回答
0
投票

video_comment
表有一个引用
video
表的外键。这意味着要在
video_comment
中插入一行,
video
必须先已存在。

您应该交换 INSERT 语句的顺序,以便将相应的

video
comment
行插入到
video_comment
行之前。

# I moved this line to insert the video before the video_comment:
db.execute("INSERT INTO video (user_id,video_id,data,url) VALUES (?,?,?,?)", 1,1, current_time, url)

for elemen in comments:
    print(elemen.text)
    db.execute("INSERT INTO comments (user_id, comment,data,url) VALUES (?,?,?,?)", 1, elemen.text,current_time, url)
    comment_id = db.execute("SELECT id FROM comments WHERE comment = ?", elemen.text)[0]
    print(comment_id)
    print(comment_id['id'])

    db.execute("INSERT INTO video_comment (video_id,comment_id) VALUES (1,?)", int(comment_id['id']))

这将防止“FOREIGN KEY约束失败”错误。


改进代码的一些其他建议:

  • 考虑将所有这些插入包含在事务中
  • 如果每条评论都可以链接到一个视频,则更好的架构可能是在
    video_id
    表中包含
    comment
    列,并删除
    video_comment
    表。从技术上讲,您当前的架构允许将评论链接到多个视频,这可能不是您想要的。
© www.soinside.com 2019 - 2024. All rights reserved.