From 5e0be0873ca6fe0c4ad7abe1398443bdf367d16e Mon Sep 17 00:00:00 2001 From: sag᠎e Date: Tue, 31 Jan 2023 10:52:07 +0000 Subject: [4.2.x] Fixed #34304 -- Made MySQL's SchemaEditor.remove_constraint() don't create foreign key index when unique constraint is ignored. Regression in b731e8841558ee4caaba766c83f34ea9c7004f8b. Backport of 110b3b83567da22f19ec04210db134d0fe83d662 from main --- tests/schema/tests.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/schema/tests.py b/tests/schema/tests.py index e325fa0c88..c0f0e52a63 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2676,6 +2676,37 @@ class SchemaTests(TransactionTestCase): with self.assertRaises(IntegrityError): Tag.objects.create(title="bar", slug="foo") + def test_remove_ignored_unique_constraint_not_create_fk_index(self): + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(Book) + constraint = UniqueConstraint( + "author", + condition=Q(title__in=["tHGttG", "tRatEotU"]), + name="book_author_condition_uniq", + ) + # Add unique constraint. + with connection.schema_editor() as editor: + editor.add_constraint(Book, constraint) + old_constraints = self.get_constraints_for_column( + Book, + Book._meta.get_field("author").column, + ) + # Remove unique constraint. + with connection.schema_editor() as editor: + editor.remove_constraint(Book, constraint) + new_constraints = self.get_constraints_for_column( + Book, + Book._meta.get_field("author").column, + ) + # Redundant foreign key index is not added. + self.assertEqual( + len(old_constraints) - 1 + if connection.features.supports_partial_indexes + else len(old_constraints), + len(new_constraints), + ) + @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields") def test_remove_field_unique_does_not_remove_meta_constraints(self): with connection.schema_editor() as editor: -- cgit v1.3