snippetpythondjangoCriticalCanonical
How can I temporarily disable a foreign key constraint in MySQL?
Viewed 0 times
keyforeignconstrainthowdisablemysqlcantemporarily
Problem
Is it possible to temporarily disable constraints in MySQL?
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:
Is it possible to temporarily disable constraints and delete anyway?
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?
Solution
Try
Make sure to
after.
DISABLE KEYS orSET FOREIGN_KEY_CHECKS=0;Make sure to
SET FOREIGN_KEY_CHECKS=1;after.
Code Snippets
SET FOREIGN_KEY_CHECKS=0;SET FOREIGN_KEY_CHECKS=1;Context
Stack Overflow Q#15501673, score: 1855
Revisions (0)
No revisions yet.