Opened 112 minutes ago
Last modified 88 minutes ago
#37551 assigned Bug
Renaming a field with db_column on one model makes makemigrations generate AlterConstraint for a changed constraint on another model
| Reported by: | Konstantinos Giannopoulos | Owned by: | Konstantinos Giannopoulos |
|---|---|---|---|
| Component: | Migrations | Version: | 6.0 |
| Severity: | Release blocker | Keywords: | AlterConstraint, db_column, RenameField, autodetector |
| Cc: | Konstantinos Giannopoulos | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When a field is renamed while keeping the same db_column, and in the same makemigrations run a constraint on a different model changes from the old field name to the new field name, the autodetector generates AlterConstraint instead of RemoveConstraint + AddConstraint. AlterConstraint is a database no-op, so the database keeps the old constraint, and makemigrations --check reports no changes afterwards.
Initial models (migrated):
class Publisher(models.Model): code = models.CharField(max_length=20, db_column="code") class Book(models.Model): code = models.CharField(max_length=30) slug = models.SlugField() class Meta: constraints = [ models.UniqueConstraint(fields=["code"], name="unique_book_identifier"), ]
Rename Publisher.code to Publisher.slug (same db_column), and move the unrelated Book constraint from code to slug:
class Publisher(models.Model): slug = models.CharField(max_length=20, db_column="code") class Book(models.Model): code = models.CharField(max_length=30) slug = models.SlugField() class Meta: constraints = [ models.UniqueConstraint(fields=["slug"], name="unique_book_identifier"), ]
Running makemigrations and answering "y" to the rename question generates:
Migrations for 'catalog':
catalog/migrations/0002_rename_code_publisher_slug_and_more.py
~ Rename field code on publisher to slug
~ Alter constraint unique_book_identifier on book
sqlmigrate shows both operations as -- (no-op). After migrate, catalog_book still has UNIQUE ("code") and no unique constraint on slug: rows with duplicate slug values are accepted, and rows with duplicate code values are rejected. If the Publisher rename is left out (or made in a separate makemigrations run), RemoveConstraint + AddConstraint are generated as expected.
The same happens when the two models are in different apps: with Publisher in a publishers app and Book in a catalog app, renaming Publisher.code makes the catalog migration contain only the no-op AlterConstraint, with the same result in the database.
MigrationAutodetector._constraint_should_be_dropped_and_recreated() replaces renamed field names in the new constraint's fields for every entry in self.renamed_operations, but ignores the app label and model name of each rename. Publisher's code -> slug rename is therefore applied to Book's constraint, which then compares equal to the old one.
Regression in b2407e4d7d25e4a5839bb512cb2fdf71e9e30a21 (#35305). Before that commit, RemoveConstraint + AddConstraint were generated for this case.
A fix could pass the constraint's app_label and model_name to _constraint_should_be_dropped_and_recreated() and skip renames from other models. Regression test for tests/migrations/test_autodetector.py:
def test_rename_field_preserve_db_column_other_model_constraint(self): publisher = ModelState( "app", "Publisher", [("id", models.AutoField(primary_key=True)), ("code", models.CharField(max_length=20, db_column="code"))], ) publisher_renamed = ModelState( "app", "Publisher", [("id", models.AutoField(primary_key=True)), ("slug", models.CharField(max_length=20, db_column="code"))], ) def book(field_name): return ModelState( "app", "Book", [("id", models.AutoField(primary_key=True)), ("code", models.CharField(max_length=30)), ("slug", models.SlugField())], options={"constraints": [ models.UniqueConstraint(fields=[field_name], name="unique_book_identifier"), ]}, ) changes = self.get_changes( [publisher, book("code")], [publisher_renamed, book("slug")], MigrationQuestioner({"ask_rename": True}), ) self.assertOperationTypes( changes, "app", 0, ["RemoveConstraint", "RenameField", "AddConstraint"] )
And the same with the models in different apps:
def test_rename_field_preserve_db_column_other_app_constraint(self): def publisher(field_name): return ModelState( "publishers", "Publisher", [("id", models.AutoField(primary_key=True)), (field_name, models.CharField(max_length=20, db_column="code"))], ) def book(field_name): return ModelState( "catalog", "Book", [("id", models.AutoField(primary_key=True)), ("code", models.CharField(max_length=30)), ("slug", models.SlugField())], options={"constraints": [ models.UniqueConstraint(fields=[field_name], name="unique_book_identifier"), ]}, ) changes = self.get_changes( [publisher("code"), book("code")], [publisher("slug"), book("slug")], MigrationQuestioner({"ask_rename": True}), ) self.assertOperationTypes(changes, "publishers", 0, ["RenameField"]) self.assertOperationTypes( changes, "catalog", 0, ["RemoveConstraint", "AddConstraint"] )
A small project reproducing this end to end is attached.
Attachments (1)
Change History (3)
by , 102 minutes ago
| Attachment: | sample_bookstore_app.zip added |
|---|
comment:1 by , 100 minutes ago
| Has patch: | unset |
|---|
If this ticket is accepted, i will work on a PR for a fix.
This is also related to ticket #37552