Fixed #22172 -- Allowed index_together to be a single list (rather than list of lists)..

Thanks EmilStenstrom for the suggestion.
This commit is contained in:
Anubhav Joshi 2014-03-02 00:36:15 +05:30 committed by Tim Graham
parent 3273bd7b25
commit bb2ca9fe6c
10 changed files with 67 additions and 19 deletions

View file

@ -12,6 +12,14 @@ class Article(models.Model):
]
# Model for index_together being used only with single list
class IndexTogetherSingleList(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateTimeField()
class Meta:
index_together = ["headline", "pub_date"]
# Indexing a TextField on Oracle or MySQL results in index creation error.
if connection.vendor == 'postgresql':
class IndexedArticle(models.Model):

View file

@ -4,7 +4,7 @@ from django.core.management.color import no_style
from django.db import connections, DEFAULT_DB_ALIAS
from django.test import TestCase
from .models import Article
from .models import Article, IndexTogetherSingleList
class IndexesTests(TestCase):
@ -13,6 +13,12 @@ class IndexesTests(TestCase):
index_sql = connection.creation.sql_indexes_for_model(Article, no_style())
self.assertEqual(len(index_sql), 1)
def test_index_together_single_list(self):
# Test for using index_together with a single list (#22172)
connection = connections[DEFAULT_DB_ALIAS]
index_sql = connection.creation.sql_indexes_for_model(IndexTogetherSingleList, no_style())
self.assertEqual(len(index_sql), 1)
@skipUnless(connections[DEFAULT_DB_ALIAS].vendor == 'postgresql',
"This is a postgresql-specific issue")
def test_postgresql_text_indexes(self):

View file

@ -45,10 +45,7 @@ class IndexTogetherTests(IsolatedModelsTestCase):
def test_list_containing_non_iterable(self):
class Model(models.Model):
class Meta:
index_together = [
'non-iterable',
'second-non-iterable',
]
index_together = [('a', 'b'), 42]
errors = Model.check()
expected = [
@ -139,6 +136,22 @@ class UniqueTogetherTests(IsolatedModelsTestCase):
]
self.assertEqual(errors, expected)
def test_non_list(self):
class Model(models.Model):
class Meta:
unique_together = 'not-a-list'
errors = Model.check()
expected = [
Error(
'"unique_together" must be a list or tuple.',
hint=None,
obj=Model,
id='E008',
),
]
self.assertEqual(errors, expected)
def test_valid_model(self):
class Model(models.Model):
one = models.IntegerField()

View file

@ -25,6 +25,7 @@ class StateTests(TestCase):
app_label = "migrations"
apps = new_apps
unique_together = ["name", "bio"]
index_together = ["bio", "age"]
class AuthorProxy(Author):
class Meta:
@ -63,7 +64,7 @@ class StateTests(TestCase):
self.assertEqual(author_state.fields[1][1].max_length, 255)
self.assertEqual(author_state.fields[2][1].null, False)
self.assertEqual(author_state.fields[3][1].null, True)
self.assertEqual(author_state.options, {"unique_together": set([("name", "bio")])})
self.assertEqual(author_state.options, {"unique_together": set([("name", "bio")]), "index_together": set([("bio", "age")])})
self.assertEqual(author_state.bases, (models.Model, ))
self.assertEqual(book_state.app_label, "migrations")