mirror of
https://github.com/django/django.git
synced 2025-10-29 08:36:09 +00:00
[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 of6bf7964023from master
This commit is contained in:
committed by
Tim Graham
parent
5c04852455
commit
198128684b
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user