summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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: