summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorAaron Elliot Ross <aaronelliotross@gmail.com>2016-06-28 14:22:20 +0200
committerTim Graham <timograham@gmail.com>2016-06-28 08:22:36 -0400
commit198128684bb0c47e9ea2900a92d192794d62791f (patch)
treea477fd5e531a5607496d8896c1ca8c4102c9010b /django/db
parent5c04852455d7b08314da6d1c156819ff0dfbec3a (diff)
[1.10.x] Fixed #26171 -- Made MySQL create an index on ForeignKeys with db_contraint=False.
Refactored "Prevented unneeded index creation on MySQL-InnoDB" (2ceb10f) to avoid setting db_index=False. Backport of 6bf7964023487f2a352084e74aca27aecb354d6c from master
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/base/schema.py5
-rw-r--r--django/db/backends/mysql/schema.py19
2 files changed, 15 insertions, 9 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 156f119c0d..e06b29f3db 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -869,7 +869,7 @@ class BaseDatabaseSchemaEditor(object):
return []
output = []
for field in model._meta.local_fields:
- if field.db_index and not field.unique:
+ if self._field_should_be_indexed(model, field):
output.append(self._create_index_sql(model, [field], suffix=""))
for field_names in model._meta.index_together:
@@ -877,6 +877,9 @@ class BaseDatabaseSchemaEditor(object):
output.append(self._create_index_sql(model, fields, suffix="_idx"))
return output
+ def _field_should_be_indexed(self, model, field):
+ return field.db_index and not field.unique
+
def _rename_field_sql(self, table, old_field, new_field, new_type):
return self.sql_rename_column % {
"table": self.quote_name(table),
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index 802ce6353b..4aa40f7cb5 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -51,17 +51,20 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
'column': self.quote_name(field.column),
}, [effective_default])
- def _model_indexes_sql(self, model):
+ def _field_should_be_indexed(self, model, field):
+ create_index = super(DatabaseSchemaEditor, self)._field_should_be_indexed(model, field)
storage = self.connection.introspection.get_storage_engine(
self.connection.cursor(), model._meta.db_table
)
- if storage == "InnoDB":
- for field in model._meta.local_fields:
- if field.db_index and not field.unique and field.get_internal_type() == "ForeignKey":
- # Temporary setting db_index to False (in memory) to disable
- # index creation for FKs (index automatically created by MySQL)
- field.db_index = False
- return super(DatabaseSchemaEditor, self)._model_indexes_sql(model)
+ # No need to create an index for ForeignKey fields except if
+ # db_constraint=False because the index from that constraint won't be
+ # created.
+ if (storage == "InnoDB" and
+ create_index and
+ field.get_internal_type() == 'ForeignKey' and
+ field.db_constraint):
+ return False
+ return create_index
def _delete_composed_index(self, model, fields, *args):
"""