#26384: Django 1.9 regression in migrations due to lazy model operations 
refactor
---------------------------------+------------------------------------
     Reporter:  brian            |                    Owner:  nobody
         Type:  Bug              |                   Status:  new
    Component:  Migrations       |                  Version:  1.9
     Severity:  Release blocker  |               Resolution:
     Keywords:                   |             Triage Stage:  Accepted
    Has patch:  1                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+------------------------------------
Changes (by AlexHill):

 * has_patch:  0 => 1


Comment:

 The model in Tim's test case looks like this:

 {{{
 #!python
 class Foo(models.Model):
     # album_id = models.AutoField(primary_key=True) (renamed to `id` in
 migration 2)
     parent = models.ForeignKey('self', null=True, blank=True)
 }}}

 The problematic migration wants to rename `Foo.album_id` to `Foo.id`. To
 perform this kind of schema alteration under SQLite, we have to create a
 new model with updated fields, and from that model make a new table, copy
 the data, then remove the old table. The action happens in
 
[https://github.com/django/django/blob/67cf5efa31acb2916034afb15610b700695dfcb0/django/db/backends/sqlite3/schema.py#L68
 db.backends.sqlite3.schema.DatabaseSchemaEditor._remake_table()].

 The bug occurs because only the AutoField is changed, while the ForeignKey
 is considered not to have changed and is copied directly from the pre-
 migration model. In fact the ForeignKey, in its `remote_field.field_name`,
 holds a copy of the AutoField's field name, so it needs to be updated too
 to ensure the temporary model is internally consistent.

 To fix the bug we can re-create a model's self-referential related fields
 using Field.clone() rather than just using the old model's field
 instances.

 I've opened a PR at https://github.com/django/django/pull/6355.

--
Ticket URL: <https://code.djangoproject.com/ticket/26384#comment:9>
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/063.f803de66d7e56b5ec7dc3ff62030b979%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to