﻿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
32263	squashmigrations produces incorrect result with a RenameModel on a ForeignKey target.	InvalidInterrupt	Bhuvnesh	"Given a list of operations:
{{{
    operations = [
        migrations.CreateModel(
            name=""Author"",
            fields=[
                (""id"",
                 models.AutoField(auto_created=True, primary_key=True, serialize=False,
                                  verbose_name=""ID"")),
            ]
        ),
        migrations.CreateModel(
            name=""Book"",
            fields=[
                (""id"",
                 models.AutoField(auto_created=True, primary_key=True, serialize=False,
                                  verbose_name=""ID"")),
                (""author"", models.ForeignKey(on_delete=models.deletion.PROTECT,
                                             to=""testapp.Author"")),
            ]
        ),
        migrations.RenameModel(""Author"", ""Person""),
        migrations.AddField(model_name=""person"",
                            name=""birthdate"",
                            field=models.DateField()),
    ]
}}}

squashmigrations produces the result:
{{{
    operations = [
        migrations.CreateModel(
            name='Book',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='testapp.author')),
            ],
        ),
        migrations.CreateModel(
            name='Person',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('birthdate', models.DateField()),
            ],
        ),
    ]
}}}

There are two apparent problems with the result:
* The model with the `ForeignKey` field is created before the target of the `ForeignKey`
* The `to` parameter of the `ForeignKey` is not updated with the new model name (the final `AddField` operation is not necessary to cause this problem)

I believe this is caused by `CreateModel.reduce()` allowing optimization though operations that reference models which the model being created has a relationship to. Similar effects are likely to be caused by other migration patterns as well (I think Tim Graham may have found one [https://code.djangoproject.com/ticket/24849#comment:1 here] but as far as I can tell that's not the problem originally reported in that ticket)"	Bug	assigned	Migrations	3.1	Normal			InvalidInterrupt Bhuvnesh David Wobrock	Accepted	1	0	0	1	0	0
