From dd2e4d7b5d6f24f33c2805b0bfb97a08e27b2125 Mon Sep 17 00:00:00 2001 From: Ed Morley Date: Thu, 1 Dec 2016 15:54:58 +0000 Subject: [PATCH] Refs #27558 -- Added test for no index on InnoDB ForeignKey. The refactor in 3f76d1402dac9c2993d588f996dc1c331edbc9a7 fixed the creation of redundant indexes. Forwardport of 82ce55dbbe2d96e8b5d1fcb4a1d52b73e08e7929 from stable/1.10.x --- docs/releases/1.10.4.txt | 3 +++ tests/indexes/tests.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/docs/releases/1.10.4.txt b/docs/releases/1.10.4.txt index 86ce644fdf..5ac17ec38a 100644 --- a/docs/releases/1.10.4.txt +++ b/docs/releases/1.10.4.txt @@ -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`). diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index 3ef6d2939b..81e2278b19 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -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 @@ -98,3 +100,15 @@ class SchemaIndexesTests(TestCase): 'CREATE INDEX `indexes_articletranslation_article_no_constraint_id_d6c0806b` ' '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_articletrans_new_foreign_key_id_d27a9146_fk_indexes_a` ' + 'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)' + ])