Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#1373 closed defect (fixed)

[patch] magic-removal: MySQL does not support DROP CONSTRAINT

Reported by: Jan Rademaker <j.rademaker@…> Owned by: adrian
Component: Core (Management commands) Version: magic-removal
Severity: major Keywords: mysql constraint foreign
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Although MySQL 4 & 5 support ADD CONSTRAINT they don't support DROP CONSTRAINT. This causes manage sqlreset [app] and manage sqlclear [app] to produce invalid sql statements.

MySQL MaxDB, however, _does_ support DROP CONSTRAINT.

See #1082 also.

Attachments (2)

fix_drop_foreignkey.diff (2.8 KB) - added by Geert Vanderkelen <geert@…> 10 years ago.
newfix_drop_foreignkey.diff (2.7 KB) - added by njharman@… 9 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 10 years ago by pb@…

I'm not attaching this as a patch file because it's a hack, not a fix, but if you just need to get sqlreset and sqlclear working with &!@$# MySQL today, then:

Index: django/db/backends/mysql/
--- django/db/backends/mysql/    (revision 2545)
+++ django/db/backends/mysql/    (working copy)
@@ -86,7 +86,7 @@
             self.connection = None
-supports_constraints = True
+supports_constraints = False
 def quote_name(name):
     if name.startswith("`") and name.endswith("`"):

A slightly more granular fix would be to break down this attribute into supports_constraint_add and supports_constraint_drop, then make the appropriate updates in the backend files. The differences between MySQL MaxDB will still need to be addressed, though.

comment:2 Changed 10 years ago by Russell Cloran <russell@…>

I'm having a similar problem. (At the moment worked around by ./ sqlreset app | sed 's/DROP CONSTRAINT/DROP FOREIGN KEY/' | mysql). I have no idea how to fix it, just thought some confirmation would be good :)

comment:3 Changed 10 years ago by Geert Vanderkelen <geert@…>

  • Keywords mysql constraint foreign added
  • Severity changed from normal to major
  • Summary changed from magic-removal: MySQL does not support DROP CONSTRAINT to [patch] magic-removal: MySQL does not support DROP CONSTRAINT

I'm attaching a patch which fixes this problem. It will use the 'get_foreignkey_drop' function from the backend module so different syntax is supported. In MySQL this is ALTER TABLE <table> DROP FOREIGN KEY <name>;.

I took the liberty to also add these functions to the other backends, but I couldn't test those. Although I think it should work fine, might be good to test them ;)

I put severity to major, since this basicly makes the sqlreset for example, not work for MySQL.

Changed 10 years ago by Geert Vanderkelen <geert@…>

Changed 9 years ago by njharman@…

comment:4 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [2732]) magic-removal: Fixed #1373 -- Factored out database-specific 'DROP CONSTRAINT' syntax, to get sqlreset/sqlclear working with MySQL. Thanks, njharman

Note: See TracTickets for help on using tickets.
Back to Top