From 166a3b32632c141541d1c3f0eff18e1d8b389404 Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Wed, 24 Aug 2022 10:10:56 -0300 Subject: [PATCH] Fixed #33953 -- Reverted "Fixed #33201 -- Made RenameModel operation a noop for models with db_table." Regression in afeafd6036616bac8263d762c1610f22241c0187. This reverts afeafd6036616bac8263d762c1610f22241c0187. Thanks Timothy Thomas for the report. --- django/db/migrations/operations/models.py | 11 ++++----- docs/releases/4.1.1.txt | 3 +++ tests/migrations/test_operations.py | 28 ++++++++++------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 75a3b8b030..3a33b4aff7 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -371,13 +371,12 @@ class RenameModel(ModelOperation): new_model = to_state.apps.get_model(app_label, self.new_name) if self.allow_migrate_model(schema_editor.connection.alias, new_model): old_model = from_state.apps.get_model(app_label, self.old_name) - old_db_table = old_model._meta.db_table - new_db_table = new_model._meta.db_table - # Don't alter when a table name is not changed. - if old_db_table == new_db_table: - return # Move the main table - schema_editor.alter_db_table(new_model, old_db_table, new_db_table) + schema_editor.alter_db_table( + new_model, + old_model._meta.db_table, + new_model._meta.db_table, + ) # Alter the fields pointing to us for related_object in old_model._meta.related_objects: if related_object.related_model == old_model: diff --git a/docs/releases/4.1.1.txt b/docs/releases/4.1.1.txt index 2ae2ecfb1d..3c218274f2 100644 --- a/docs/releases/4.1.1.txt +++ b/docs/releases/4.1.1.txt @@ -46,3 +46,6 @@ Bugfixes * Fixed a migration crash on ``ManyToManyField`` fields with ``through`` referencing models in different apps (:ticket:`33938`). + +* Fixed a regression in Django 4.1 that caused an incorrect migration when + renaming a model with ``ManyToManyField`` and ``db_table`` (:ticket:`33953`). diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index f2edf6ac73..c83e333136 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1058,8 +1058,8 @@ class OperationTests(OperationTestBase): Pony._meta.get_field("riders").remote_field.through.objects.count(), 2 ) - def test_rename_model_with_db_table_noop(self): - app_label = "test_rmwdbtn" + def test_rename_model_with_db_table_rename_m2m(self): + app_label = "test_rmwdbrm2m" project_state = self.apply_operations( app_label, ProjectState(), @@ -1069,32 +1069,28 @@ class OperationTests(OperationTestBase): fields=[ ("id", models.AutoField(primary_key=True)), ], - options={"db_table": "rider"}, ), migrations.CreateModel( "Pony", fields=[ ("id", models.AutoField(primary_key=True)), - ( - "rider", - models.ForeignKey("%s.Rider" % app_label, models.CASCADE), - ), + ("riders", models.ManyToManyField("Rider")), ], + options={"db_table": "pony"}, ), ], ) new_state = project_state.clone() - operation = migrations.RenameModel("Rider", "Runner") + operation = migrations.RenameModel("Pony", "PinkPony") operation.state_forwards(app_label, new_state) + with connection.schema_editor() as editor: + operation.database_forwards(app_label, editor, project_state, new_state) - with connection.schema_editor() as editor: - with self.assertNumQueries(0): - operation.database_forwards(app_label, editor, project_state, new_state) - with connection.schema_editor() as editor: - with self.assertNumQueries(0): - operation.database_backwards( - app_label, editor, new_state, project_state - ) + Pony = new_state.apps.get_model(app_label, "PinkPony") + Rider = new_state.apps.get_model(app_label, "Rider") + pony = Pony.objects.create() + rider = Rider.objects.create() + pony.riders.add(rider) def test_rename_m2m_target_model(self): app_label = "test_rename_m2m_target_model"