1
0
mirror of https://github.com/django/django.git synced 2025-03-25 16:50:45 +00:00

[1.11.x] 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.

Backport of f79d9a322c6008e5fada1453aebfb56afc316cc8 from master
This commit is contained in:
Mariusz Felisiak 2017-12-05 21:05:10 +01:00
parent 47681af344
commit 3e52fd7595
3 changed files with 18 additions and 1 deletions

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import hashlib import hashlib
from django.db.backends.utils import split_identifier
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
__all__ = [str('Index')] __all__ = [str('Index')]
@ -101,7 +102,7 @@ class Index(object):
(8 chars) and unique hash + suffix (10 chars). Each part is made to (8 chars) and unique hash + suffix (10 chars). Each part is made to
fit its size by truncating the excess length. 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 = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders]
column_names_with_order = [ column_names_with_order = [
(('-%s' if order else '%s') % column_name) (('-%s' if order else '%s') % column_name)

View File

@ -11,3 +11,6 @@ Bugfixes
* Fixed a regression in Django 1.11 that added newlines between ``MultiWidget``'s * Fixed a regression in Django 1.11 that added newlines between ``MultiWidget``'s
subwidgets (:ticket:`28890`). subwidgets (:ticket:`28890`).
* Fixed incorrect class-based model index name generation for models with
quoted ``db_table`` (:ticket:`28876`).

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test.utils import isolate_apps
from .models import Book, ChildModel1, ChildModel2 from .models import Book, ChildModel1, ChildModel2
@ -69,6 +70,18 @@ class IndexesTests(SimpleTestCase):
with self.assertRaisesMessage(AssertionError, msg): with self.assertRaisesMessage(AssertionError, msg):
long_field_index.set_name_with_model(Book) 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): def test_deconstruction(self):
index = models.Index(fields=['title']) index = models.Index(fields=['title'])
index.set_name_with_model(Book) index.set_name_with_model(Book)