mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
[1.10.x] Fixed #27558 -- Prevented redundant index on InnoDB ForeignKey.
The MySQL backend overrides _field_should_be_indexed() so that it skips index creation for ForeignKeys when using InnoDB.
This commit is contained in:
parent
4074fa9145
commit
82ce55dbbe
@ -418,7 +418,7 @@ class BaseDatabaseSchemaEditor(object):
|
||||
}
|
||||
self.execute(sql)
|
||||
# Add an index, if required
|
||||
if field.db_index and not field.unique:
|
||||
if self._field_should_be_indexed(model, field):
|
||||
self.deferred_sql.append(self._create_index_sql(model, [field]))
|
||||
# Add any FK constraints later
|
||||
if field.remote_field and self.connection.features.supports_foreign_keys and field.db_constraint:
|
||||
|
@ -26,3 +26,6 @@ Bugfixes
|
||||
|
||||
* Prevented ``LocaleMiddleware`` from redirecting on URLs that should return
|
||||
404 when using ``prefix_default_language=False`` (:ticket:`27402`).
|
||||
|
||||
* Prevented an unnecessary index from being created on an InnoDB ``ForeignKey``
|
||||
when the field was added after the model was created (:ticket:`27558`).
|
||||
|
@ -1,6 +1,8 @@
|
||||
from unittest import skipUnless
|
||||
|
||||
from django.db import connection
|
||||
from django.db.models.deletion import CASCADE
|
||||
from django.db.models.fields.related import ForeignKey
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Article, ArticleTranslation, IndexTogetherSingleList
|
||||
@ -74,3 +76,15 @@ class SchemaIndexesTests(TestCase):
|
||||
'CREATE INDEX `indexes_articletranslation_99fb53c2` '
|
||||
'ON `indexes_articletranslation` (`article_no_constraint_id`)'
|
||||
])
|
||||
|
||||
# The index also shouldn't be created if the ForeignKey is added after
|
||||
# the model was created.
|
||||
with connection.schema_editor() as editor:
|
||||
new_field = ForeignKey(Article, CASCADE)
|
||||
new_field.set_attributes_from_name('new_foreign_key')
|
||||
editor.add_field(ArticleTranslation, new_field)
|
||||
self.assertEqual(editor.deferred_sql, [
|
||||
'ALTER TABLE `indexes_articletranslation` '
|
||||
'ADD CONSTRAINT `indexes_articl_new_foreign_key_id_d27a9146_fk_indexes_article_id` '
|
||||
'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)'
|
||||
])
|
||||
|
Loading…
Reference in New Issue
Block a user