#34881: migrate crashes when renaming model referenced twice by
ManyToManyField.through model on SQLite.
----------------------------+----------------------------------------
     Reporter:  dennisvang  |                    Owner:  Jase Hackman
         Type:  Bug         |                   Status:  assigned
    Component:  Migrations  |                  Version:  dev
     Severity:  Normal      |               Resolution:
     Keywords:  sqlite      |             Triage Stage:  Accepted
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+----------------------------------------

Comment (by Simon Charette):

 Thanks for working on this patch during DjangConUS Jase!

 From reviewing your changes and tests I get a sense that there might be an
 even less invasive way to address this particular issue.

 The crux of the problem here, as you've identified it, is the SQLite
 backend needs to rebuild the table entirely and it cannot be done from an
 old representation of the model iteratively.

 By the time `alter_field` is called the columns of the tables related to
 the one being renamed are already altered in the database (a `RENAME`
 operation repoints all referencing columns) so it should be safe to
 provide the model originating from `to_state`
 
[https://github.com/django/django/blob/fdd1323b9c83e56184e0c992af8faf8d54327775/django/db/migrations/operations/models.py#L421
 like we do when dealing with self-referencing related objects].

 By retrieving the model meant to be passed to `alter_field` from to
 `to_state`

 {{{#!diff
 diff --git a/django/db/migrations/operations/models.py
 b/django/db/migrations/operations/models.py
 index d616cafb45..83e3fb772d 100644
 --- a/django/db/migrations/operations/models.py
 +++ b/django/db/migrations/operations/models.py
 @@ -421,11 +421,11 @@ def database_forwards(self, app_label,
 schema_editor, from_state, to_state):
                      model = new_model
                      related_key = (app_label, self.new_name_lower)
                  else:
 -                    model = related_object.related_model
                      related_key = (
                          related_object.related_model._meta.app_label,
                          related_object.related_model._meta.model_name,
                      )
 +                    model = to_state.apps.get_model(*related_key)
                  to_field =
 to_state.apps.get_model(*related_key)._meta.get_field(
                      related_object.field.name
                  )
 }}}

 The tests seem to be passing.

 I think that your work on `alter_fields` might be beneficial to support
 tickets like #24203 but might not be strictly necessary to address this
 particular issue.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34881#comment:17>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018b653a515e-eddf419d-f1bc-4b30-9b16-82459f63a824-000000%40eu-central-1.amazonses.com.

Reply via email to