From f79d9a322c6008e5fada1453aebfb56afc316cc8 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 5 Dec 2017 21:05:10 +0100 Subject: [PATCH] Refs #28876 -- Fixed incorrect class-based model index name generation for models with quoted db_table. Thanks Simon Charette and Tim Graham for the review and Carlos E. C. Leite for the report. --- django/db/models/indexes.py | 3 ++- docs/releases/1.11.9.txt | 3 +++ docs/releases/2.0.1.txt | 3 +++ tests/model_indexes/tests.py | 13 +++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index 73c516532e..fc24433a33 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -1,5 +1,6 @@ import hashlib +from django.db.backends.utils import split_identifier from django.utils.encoding import force_bytes __all__ = ['Index'] @@ -90,7 +91,7 @@ class Index: (8 chars) and unique hash + suffix (10 chars). Each part is made to fit its size by truncating the excess length. """ - table_name = model._meta.db_table + _, table_name = split_identifier(model._meta.db_table) column_names = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders] column_names_with_order = [ (('-%s' if order else '%s') % column_name) diff --git a/docs/releases/1.11.9.txt b/docs/releases/1.11.9.txt index 20d587248f..c7cca51818 100644 --- a/docs/releases/1.11.9.txt +++ b/docs/releases/1.11.9.txt @@ -11,3 +11,6 @@ Bugfixes * Fixed a regression in Django 1.11 that added newlines between ``MultiWidget``'s subwidgets (:ticket:`28890`). + +* Fixed incorrect class-based model index name generation for models with + quoted ``db_table`` (:ticket:`28876`). diff --git a/docs/releases/2.0.1.txt b/docs/releases/2.0.1.txt index 09b52ec4fc..cc90e4ef6e 100644 --- a/docs/releases/2.0.1.txt +++ b/docs/releases/2.0.1.txt @@ -11,3 +11,6 @@ Bugfixes * Fixed a regression in Django 1.11 that added newlines between ``MultiWidget``'s subwidgets (:ticket:`28890`). + +* Fixed incorrect class-based model index name generation for models with + quoted ``db_table`` (:ticket:`28876`). diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py index 5f1743439c..28759dbe80 100644 --- a/tests/model_indexes/tests.py +++ b/tests/model_indexes/tests.py @@ -1,6 +1,7 @@ from django.conf import settings from django.db import connection, models from django.test import SimpleTestCase, skipUnlessDBFeature +from django.test.utils import isolate_apps from .models import Book, ChildModel1, ChildModel2 @@ -70,6 +71,18 @@ class IndexesTests(SimpleTestCase): with self.assertRaisesMessage(AssertionError, msg): long_field_index.set_name_with_model(Book) + @isolate_apps('model_indexes') + def test_name_auto_generation_with_quoted_db_table(self): + class QuotedDbTable(models.Model): + name = models.CharField(max_length=50) + + class Meta: + db_table = '"t_quoted"' + + index = models.Index(fields=['name']) + index.set_name_with_model(QuotedDbTable) + self.assertEqual(index.name, 't_quoted_name_e4ed1b_idx') + def test_deconstruction(self): index = models.Index(fields=['title'], db_tablespace='idx_tbls') index.set_name_with_model(Book)