2
0
Эх сурвалжийг харах

Refs #24686 -- Made AlterField operation a noop when renaming related model with db_table.

devilsautumn 1 жил өмнө
parent
commit
f05cc5e3d2

+ 10 - 0
django/db/backends/base/schema.py

@@ -1601,10 +1601,20 @@ class BaseDatabaseSchemaEditor:
         # - changing an attribute that doesn't affect the schema
         # - changing an attribute in the provided set of ignored attributes
         # - adding only a db_column and the column name is not changed
+        # - db_table does not change for model referenced by foreign keys
         for attr in ignore.union(old_field.non_db_attrs):
             old_kwargs.pop(attr, None)
         for attr in ignore.union(new_field.non_db_attrs):
             new_kwargs.pop(attr, None)
+        if (
+            not new_field.many_to_many
+            and old_field.remote_field
+            and new_field.remote_field
+            and old_field.remote_field.model._meta.db_table
+            == new_field.remote_field.model._meta.db_table
+        ):
+            old_kwargs.pop("to", None)
+            new_kwargs.pop("to", None)
         return self.quote_name(old_field.column) != self.quote_name(
             new_field.column
         ) or (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs)

+ 12 - 0
tests/migrations/test_operations.py

@@ -997,6 +997,18 @@ class OperationTests(OperationTestBase):
         with connection.schema_editor() as editor, self.assertNumQueries(0):
             operation.database_forwards(app_label, editor, project_state, new_state)
 
+    @skipUnlessDBFeature("supports_foreign_keys")
+    def test_rename_model_with_db_table_and_fk_noop(self):
+        app_label = "test_rmwdbtfk"
+        project_state = self.set_up_test_model(
+            app_label, db_table="my_pony", related_model=True
+        )
+        new_state = project_state.clone()
+        operation = migrations.RenameModel("Pony", "LittleHorse")
+        operation.state_forwards(app_label, new_state)
+        with connection.schema_editor() as editor, self.assertNumQueries(0):
+            operation.database_forwards(app_label, editor, project_state, new_state)
+
     def test_rename_model_with_self_referential_m2m(self):
         app_label = "test_rename_model_with_self_referential_m2m"