From d3cf75ec6fbdd2c1f1bb1e6ccb48d4de1da2952b Mon Sep 17 00:00:00 2001 From: Markus Holtermann Date: Fri, 7 Apr 2017 17:47:53 +0200 Subject: [PATCH] Fixed #28051 -- Made migrations respect Index's name argument. Thanks Marc Tamlyn for the report and Tim Graham for the review. --- django/db/migrations/state.py | 3 ++- docs/releases/1.11.1.txt | 4 +++- tests/migrations/test_state.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 5c30c4c8a1..bda077f318 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -446,7 +446,8 @@ class ModelState: elif name == "indexes": indexes = [idx.clone() for idx in model._meta.indexes] for index in indexes: - index.set_name_with_model(model) + if not index.name: + index.set_name_with_model(model) options['indexes'] = indexes else: options[name] = model._meta.original_attrs[name] diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index c9f8208271..101988819a 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -9,4 +9,6 @@ Django 1.11.1 fixes several bugs in 1.11. Bugfixes ======== -* ... +* Made migrations respect ``Index``’s ``name`` argument. If you created a + named index with Django 1.11, ``makemigrations`` will create a migration to + recreate the index with the correct name (:ticket:`28051`). diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index 2029077256..cb8156fc00 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -1070,6 +1070,19 @@ class ModelStateTests(SimpleTestCase): child1_state.options['indexes'][0].name = 'bar' self.assertEqual(Child1._meta.indexes[0].name, 'migrations__name_b0afd7_idx') + @isolate_apps('migrations') + def test_explicit_index_name(self): + class TestModel(models.Model): + name = models.CharField(max_length=50) + + class Meta: + app_label = 'migrations' + indexes = [models.indexes.Index(fields=['name'], name='foo_idx')] + + model_state = ModelState.from_model(TestModel) + index_names = [index.name for index in model_state.options['indexes']] + self.assertEqual(index_names, ['foo_idx']) + class RelatedModelsTests(SimpleTestCase):