summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsag᠎e <me@laymonage.com>2023-01-31 10:52:07 +0000
committerGitHub <noreply@github.com>2023-01-31 11:52:07 +0100
commit110b3b83567da22f19ec04210db134d0fe83d662 (patch)
tree35b447924cd2b6e3ce81fedb3d15485da3bafaf4
parent40217d1a82b0c16cddba377325d12b2c253f402a (diff)
Fixed #34304 -- Made MySQL's SchemaEditor.remove_constraint() don't create foreign key index when unique constraint is ignored.
Regression in b731e8841558ee4caaba766c83f34ea9c7004f8b.
-rw-r--r--django/db/backends/mysql/schema.py5
-rw-r--r--tests/schema/tests.py31
2 files changed, 35 insertions, 1 deletions
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index 64771fe6c3..e61445e46c 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -121,7 +121,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
)
def remove_constraint(self, model, constraint):
- if isinstance(constraint, UniqueConstraint):
+ if (
+ isinstance(constraint, UniqueConstraint)
+ and constraint.create_sql(model, self) is not None
+ ):
self._create_missing_fk_index(
model,
fields=constraint.fields,
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: