﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29000	RenameModel does not rename M2M column when run after RenameField	David Nelson Adamec	Jeff	"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."	Bug	closed	Migrations	2.0	Normal	fixed			Ready for checkin	1	0	0	0	0	0
