summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorClifford Gama <cliffygamy@gmail.com>2025-12-15 23:36:24 +0200
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-16 17:45:34 -0500
commit6cc1231285a20b11058143f8cb0a6b4b3999b23a (patch)
tree4801ce95cc2211fb87db3f1c4e7041f9b8030c95 /tests/schema
parentd1a4979fa55071581411b7d62eb1ee20f2380ae3 (diff)
Fixed #36800 -- Restored ManyToManyField renaming in BaseDatabaseSchemaEditor.alter_field().
Regression in f9a44cc0fac653f8e0c2ab1cdfb12b2cc5c63fc2. Now that ManyToManyField is no longer concrete the decision of whether or not it should be altered, which is also relied on by field renaming, should take into consideration name changes even if it doesn't have a column associated with it, as auto-created many-to-many relationship table names are a base of it. Note that there is room for optimization here where a rename can be entirely avoided if ManyToManyField.db_table remains stable between .name changes, just like we do with Field.db_column remaining stable, but since this is a regression and meant to be backported the current patch focuses on correctness over further improvements. Thanks Josik for the report. Co-authored-by: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 12c14563e0..ab6cd60c07 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -2889,6 +2889,40 @@ class SchemaTests(TransactionTestCase):
def test_m2m_repoint_inherited(self):
self._test_m2m_repoint(InheritedManyToManyField)
+ def test_m2m_rename(self):
+ class LocalBook(Model):
+ authors = ManyToManyField("schema.Author")
+
+ class Meta:
+ app_label = "schema"
+ apps = new_apps
+
+ self.local_models = [LocalBook]
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ editor.create_model(LocalBook)
+ old_field = LocalBook._meta.get_field("authors")
+ new_field = ManyToManyField("schema.Author")
+ new_field.contribute_to_class(LocalBook, "writers")
+ with connection.schema_editor() as editor:
+ editor.alter_field(LocalBook, old_field, new_field, strict=True)
+ # Ensure old M2M is gone.
+ with self.assertRaises(DatabaseError):
+ self.column_classes(
+ LocalBook._meta.get_field("authors").remote_field.through
+ )
+ if connection.features.supports_foreign_keys:
+ self.assertForeignKeyExists(
+ new_field.remote_field.through,
+ "author_id",
+ "schema_author",
+ )
+ new_through_table = new_field.remote_field.through._meta.db_table
+ self.assertIn("writers", new_through_table)
+ self.assertNotIn("authors", new_through_table)
+ # Remove the old field from meta for tearDown().
+ LocalBook._meta.local_many_to_many.remove(old_field)
+
@isolate_apps("schema")
def test_m2m_rename_field_in_target_model(self):
class LocalTagM2MTest(Model):