summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Fursov <geyser85@gmail.com>2022-09-12 08:52:18 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-12 08:52:18 +0200
commitec13e801b820614ff374cb0046092caab8d67249 (patch)
tree1cab0a81b50575278873949d9459b75210b94657
parentf3cd252cfc46c0c7d66e765818dd3dadf60d4d0e (diff)
Refs #31335 -- Added SchemaEditor._create_missing_fk_index() on MySQL.
-rw-r--r--django/db/backends/mysql/schema.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index d6d303f0f0..044a752c7a 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -135,24 +135,35 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return False
return not self._is_limited_data_type(field)
- def _delete_composed_index(self, model, fields, *args):
+ def _create_missing_fk_index(
+ self,
+ model,
+ *,
+ fields,
+ ):
"""
MySQL can remove an implicit FK index on a field when that field is
covered by another index like a unique_together. "covered" here means
- that the more complex index starts like the simpler one.
- https://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
- We check here before removing the [unique|index]_together if we have to
- recreate a FK index.
+ that the more complex index has the FK field as its first field (see
+ https://bugs.mysql.com/bug.php?id=37910).
+
+ Manually create an implicit FK index to make it possible to remove the
+ composed index.
"""
first_field = model._meta.get_field(fields[0])
if first_field.get_internal_type() == "ForeignKey":
constraint_names = self._constraint_names(
- model, [first_field.column], index=True
+ model,
+ [first_field.column],
+ index=True,
)
if not constraint_names:
self.execute(
self._create_index_sql(model, fields=[first_field], suffix="")
)
+
+ def _delete_composed_index(self, model, fields, *args):
+ self._create_missing_fk_index(model, fields=fields)
return super()._delete_composed_index(model, fields, *args)
def _set_field_new_type_null_status(self, field, new_type):