#26223: Squashing migrations with preserve_default=False keeps the default
--------------------------------+------------------------------------
Reporter: bartekwojcicki | Owner: vytisb
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by vytisb):
* owner: nobody => vytisb
* status: new => assigned
Comment:
I have analyzed the migration optimizer code and identified 5 cases where
`preserve_default` is not handled correctly.
{{{
#!div style="font-size: 80%"
{{{#!python
>>> optimize([
... migrations.CreateModel("Foo", [("name",
models.CharField(max_length=255))]),
... migrations.AddField("Foo", "value",
models.IntegerField(default=42), preserve_default=False),
... ])
[
migrations.CreateModel(
name='Foo',
fields=[
('name', models.CharField(max_length=255)),
('value', models.IntegerField(default=42)),
],
),
]
}}}
}}}
{{{
#!div style="font-size: 80%"
{{{#!python
>>> optimize([
... migrations.CreateModel("Foo", [("value",
models.IntegerField(null=True))]),
... migrations.AlterField("Foo", "value",
models.IntegerField(default=42), preserve_default=False),
... ])
[
migrations.CreateModel(
name='Foo',
fields=[
('value', models.IntegerField(default=42)),
],
),
]
}}}
}}}
{{{
#!div style="font-size: 80%"
{{{#!python
>>> optimize([
... migrations.AddField("Foo", "value",
models.IntegerField(null=True)),
... migrations.AlterField("Foo", "value",
models.IntegerField(default=42), preserve_default=False),
... ])
[
migrations.AddField(
model_name='Foo',
name='value',
field=models.IntegerField(default=42),
),
]
}}}
}}}
{{{
#!div style="font-size: 80%"
{{{#!python
>>> optimize([
... migrations.AddField("Foo", "value",
models.IntegerField(default=42), preserve_default=False),
... migrations.RenameField("Foo", "value", "price"),
... ])
[
migrations.AddField(
model_name='Foo',
name='price',
field=models.IntegerField(default=42),
),
]
}}}
}}}
{{{
#!div style="font-size: 80%"
{{{#!python
>>> optimize([
... migrations.AlterField("Foo", "value",
models.IntegerField(default=42), preserve_default=False),
... migrations.RenameField("Foo", "value", "price"),
,,, ])
[
migrations.RenameField(
model_name='Foo',
old_name='value',
new_name='price',
),
migrations.AlterField(
model_name='Foo',
name='price',
field=models.IntegerField(default=42),
),
]
}}}
}}}
For the cases involving `CreateModel`, I intend to add a new property
`real_field` (suggestions for a better name are welcome) to
`AddField`/`AlterField` which would return a possibly modified field after
taking `preserve_default` into account. It would be the same field that
these operations put into state. Then `CreateModel` could use the new
property in its optimization.
For the other cases, proper passing of `preserve_default` should be
sufficient.
--
Ticket URL: <https://code.djangoproject.com/ticket/26223#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/072.4fe591b781cbf00975ce193cd18ca0af%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.