#32263: squashmigrations produces incorrect result when a RenameModel operation
is
performed on a ForeignKey target
----------------------------------+--------------------------------------
Reporter: InvalidInterrupt | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.1
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
----------------------------------+--------------------------------------
Description changed by InvalidInterrupt:
Old description:
> 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
>
> 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)
New description:
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)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32263#comment:1>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/074.2e0580b87ed7e75d613b3204800b7709%40djangoproject.com.