本文介紹了如何在 MySQL 中臨時禁用外鍵約束?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以暫時禁用 MySQL 中的約束?
Is it possible to temporarily disable constraints in MySQL?
我有兩個 Django 模型,每個模型都有一個指向另一個模型的外鍵.由于外鍵約束,刪除模型的實例會返回錯誤:
I have two Django models, each with a foreign key to the other one. Deleting instances of a model returns an error because of the foreign key constraint:
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()
是否可以暫時禁用約束并刪除?
Is it possible to temporarily disable constraints and delete anyway?
推薦答案
Try DISABLE KEYS
或
SET FOREIGN_KEY_CHECKS=0;
確保
SET FOREIGN_KEY_CHECKS=1;
之后.
這篇關于如何在 MySQL 中臨時禁用外鍵約束?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!