Opened 4 years ago
Last modified 2 months ago
#32263 assigned Bug
squashmigrations produces incorrect result when a RenameModel operation is performed on a ForeignKey target — at Version 1
Reported by: | InvalidInterrupt | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | InvalidInterrupt, Bhuvnesh, David Wobrock | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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 theForeignKey
- The
to
parameter of theForeignKey
is not updated with the new model name (the finalAddField
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 here but as far as I can tell that's not the problem originally reported in that ticket)
Change History (3)
comment:1 by , 4 years ago
Description: | modified (diff) |
---|
by , 4 years ago
Attachment: | ticket_32263_1.tar.gz added |
---|
Sample project 1.