#25253: MySQL migrations drop & recreate constraints unnecessarily when changing
attributes that don't affect the schema
--------------------------------------+------------------------------------
     Reporter:  Thomas Recouvreux     |                    Owner:  Shun Yu
         Type:  Cleanup/optimization  |                   Status:  assigned
    Component:  Migrations            |                  Version:  1.8
     Severity:  Normal                |               Resolution:
     Keywords:  migrations m2m mysql  |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Mike Edmunds):

 Still seems to be an issue in Django 2.0.5.

 The source of the problem appears to be
 BaseDatabaseSchemaEditor._alter_field in django.db.backends.base.schema.
 It ''unconditionally''
 
[https://github.com/django/django/blob/2.0.5/django/db/backends/base/schema.py#L530-L542
 drops any existing foreign key constraint(s)] and
 
[https://github.com/django/django/blob/2.0.5/django/db/backends/base/schema.py#L530-L542
 later re-creates it]—whether or not anything is changing. (It seems to be
 more selective about dropping other types of constraints only if the field
 is changing in a way that impacts them.) The
 
[https://github.com/django/django/commit/a92bae0f0622fb45afb94bf5448b49bc32ebb643
 origin of this code] is 2012, I think during initial implementation of
 Django migrations.

 Are there non-obvious field changes where the field's FK constraint needs
 to be dropped and re-created? Or some other reason this code runs
 unconditionally? Optimization around other field changes?

 A fix might be to defer dropping the FK constraint until just before the
 field's actions/null_actions/post_actions are collected and ready to be
 applied (around line 650), and then drop it only if there are actually
 actions to execute.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25253#comment:14>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.780dd0a5206dc6eb5fd736b5d643ae6c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to