#29000: RenameModel does not rename M2M column when run after
AlterField/RenameField
-------------------------------------+-------------------------------------
     Reporter:  David Nelson Adamec  |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  2.0
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by David Nelson Adamec):

 * version:  1.11 => 2.0


Old description:

> Encountered this on a project at work. I have some models like so:
>
> {{{#!python
>     class ModelA(models.Model):
>         name_renamed = models.CharField(max_length=10)
>
>     class ModelBRenamed(models.Model):
>         a = models.ForeignKey(ModelA, null=True)
>
>     class ModelC(models.Model):
>         b_set = models.ManyToManyField(ModelBRenamed)
> }}}
>
> I have a migration 0002_renamefield to rename a field on ModelA:
>
> {{{#!python
> class Migration(migrations.Migration):
>
>     dependencies = [
>         ('myapp', '0001_initial'),
>     ]
>
>     operations = [
>         migrations.RenameField(
>             model_name='modela',
>             old_name='name',
>             new_name='name_renamed',
>         ),
>     ]
> }}}
>
> Then a migration 0003_renamemodel to rename ModelB:
>
> {{{#!python
> class Migration(migrations.Migration):
>
>     dependencies = [
>         ('myapp', '0002_renamefield'),
>     ]
>
>     operations = [
>         migrations.RenameModel(
>             old_name='ModelB',
>             new_name='ModelBRenamed',
>         ),
>     ]
> }}}
>
> If the two migrations are run together, then the `modelb_id` column in
> `myapp_modelc_b_set` will not be renamed to `modelbrenamed_id`. However,
> if I run the migrations one at a time, the column will be renamed as
> expected.
>
> I think this is related to #27737. When ModelA is reloaded in the
> RenameField migration, ModelB is reloaded due to it's foreign key but is
> missing its relationship to ModelC. When the RenameModel migration is
> run, ModelC is not included in ModelB's related_objects, so the through
> table's column is not renamed.
>
> I've reproduced this using Django 1.11 on either MySQL or SQLite.

New description:

 Encountered this on a project at work. I have some models like so:

 {{{#!python
 class ModelA(models.Model):
     name_renamed = models.CharField(max_length=10)

 class ModelBRenamed(models.Model):
     a = models.ForeignKey(ModelA, null=True)

 class ModelC(models.Model):
     b_set = models.ManyToManyField(ModelBRenamed)
 }}}

 I have a migration 0002_renamefield to rename a field on ModelA:

 {{{#!python
 class Migration(migrations.Migration):

     dependencies = [
         ('myapp', '0001_initial'),
     ]

     operations = [
         migrations.RenameField(
             model_name='modela',
             old_name='name',
             new_name='name_renamed',
         ),
     ]
 }}}

 Then a migration 0003_renamemodel to rename ModelB:

 {{{#!python
 class Migration(migrations.Migration):

     dependencies = [
         ('myapp', '0002_renamefield'),
     ]

     operations = [
         migrations.RenameModel(
             old_name='ModelB',
             new_name='ModelBRenamed',
         ),
     ]
 }}}

 If the two migrations are run together, then the `modelb_id` column in
 `myapp_modelc_b_set` will not be renamed to `modelbrenamed_id`. However,
 if I run the migrations one at a time, the column will be renamed as
 expected.

 I think this is related to #27737. When ModelA is reloaded in the
 RenameField migration, ModelB is reloaded due to its foreign key but is
 missing its relationship to ModelC. When the RenameModel migration is run,
 ModelC is not included in ModelB's related_objects, so the through table's
 column is not renamed.

 I've reproduced this using Django 1.11 on either MySQL or SQLite.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29000#comment:3>
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/065.df0695d8b3122ad18d2813224d212e40%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to