【发布时间】:2021-05-09 13:03:49
【问题描述】:
我正在尝试从表“users”中删除行列表,一些 user_ids 在 user_plan 表中被引用为 FK。我想在“for循环”中跳过删除,即处理错误
x= [1,31,32,33,34]
for i in x:
cursor.execute("DELETE FROM users WHERE id=%s", (str(i),))
>>>psycopg2.errors.ForeignKeyViolation: update or delete on table "user_plan" violates foreign key constraint "user_fk_id" on table "user_plan"
DETAIL: Key (id)=(1) is still referenced from table "user_plan".
如何修改查询,以便在其他地方使用 FK 时跳过删除?
【问题讨论】:
标签: python postgresql psycopg2