Opened 9 years ago
Closed 7 years ago
#27731 closed Cleanup/optimization (fixed)
Squashmigrations doesn't optimize AlterField related_name across AlterUniqueTogether/AlterIndexTogether
| Reported by: | Ed Morley | Owned by: | Simon Charette |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | squashmigrations |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The squashmigrations command doesn't combine an AlterField() with CreateModel() in the case below, when all that was changed was the related_name.
ie: This fails on Django master...
diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py
index fc4f0ac..93a43a7 100644
--- a/tests/migrations/test_optimizer.py
+++ b/tests/migrations/test_optimizer.py
@@ -572,6 +572,26 @@ class OptimizerTests(SimpleTestCase):
],
)
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel("Bar", [("name", models.CharField(max_length=255))]),
+ migrations.CreateModel("Foo", [
+ ("a", models.ForeignKey("testapp.Bar", models.CASCADE)),
+ ("b", models.IntegerField()),
+ ]),
+ alter,
+ migrations.AlterField("Foo", "a", models.ForeignKey("testapp.Bar", models.CASCADE, related_name="baz")),
+ ],
+ [
+ migrations.CreateModel("Bar", [("name", models.CharField(max_length=255))]),
+ migrations.CreateModel("Foo", [
+ ("a", models.ForeignKey("testapp.Bar", models.CASCADE, related_name="baz")),
+ ("b", models.IntegerField()),
+ ]),
+ alter,
+ ],
+ )
+
# RenameField
self.assertDoesNotOptimize(
[
This causes failures in:
- test_create_alter_index_field
- test_create_alter_unique_field
ie optimization should occur but doesn't, when alter is:
migrations.AlterUniqueTogether("Foo", [["a", "b"]])
...or when alter is:
migrations.AlterIndexTogether("Foo", [["a", "b"]])
(But passes in the AlterOrderWithRespectTo case).
Change History (6)
comment:1 by , 9 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
Anyway I've added PR that fixes this and could be discussed.
It's a bit straightforward implementation PR
comment:4 by , 9 years ago
| Patch needs improvement: | set |
|---|
comment:5 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Patch needs improvement: | unset |
This is handled by PR which implements the CreateModel/AlterTogetherOptionOperation reduction into CreateModel.options and then allows the CreateModel/AlterField reduction into CreateModel.fields to take place.
I guess this behaviour is expected.
We have following assert in OptimizerTest
self.assertDoesNotOptimize( [ migrations.CreateModel("Foo", [ ("a", models.IntegerField()), ("b", models.IntegerField()), ]), alter, migrations.AlterField("Foo", "b", models.CharField(max_length=255)), ], )In general, if we have
AlterUniqueTogetherset for some field, let's saya, and after that performsAlterFieldon same field then we do not perform optimisation.Actually it could be fixed, but it requires some thoughts about how to determine what changes introduce this
AlterFIeld- some of them could be safe to optimise, some of them not.