是否可以在MySQL中暂时禁用约束?
我有两个Django模型,每个模型都有一个外键。由于ForeignKey约束,删除模型实例将返回错误:
cursor.execute("DELETE FROM myapp_item WHERE n = %s", n) transaction.commit_unless_managed() #a foreign key constraint fails here cursor.execute("DELETE FROM myapp_style WHERE n = %s", n) transaction.commit_unless_managed()
是否可以暂时禁用约束并仍然删除?
尝试DISABLE KEYS或
DISABLE KEYS
SET FOREIGN_KEY_CHECKS=0;
确保
SET FOREIGN_KEY_CHECKS=1;
后。