diff --git a/django/db/backends/ddl_references.py b/django/db/backends/ddl_references.py index 44e49806f81..ba55de1df8b 100644 --- a/django/db/backends/ddl_references.py +++ b/django/db/backends/ddl_references.py @@ -83,10 +83,14 @@ class Columns(TableColumns): def __str__(self): def col_str(column, idx): + col = self.quote_name(column) try: - return self.quote_name(column) + self.col_suffixes[idx] + suffix = self.col_suffixes[idx] + if suffix: + col = '{} {}'.format(col, suffix) except IndexError: - return self.quote_name(column) + pass + return col return ', '.join(col_str(column, idx) for idx, column in enumerate(self.columns)) @@ -114,7 +118,9 @@ class IndexColumns(Columns): # length as self.columns. col = '{} {}'.format(self.quote_name(column), self.opclasses[idx]) try: - col = '{} {}'.format(col, self.col_suffixes[idx]) + suffix = self.col_suffixes[idx] + if suffix: + col = '{} {}'.format(col, suffix) except IndexError: pass return col diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index 799146d6a8d..04ab036139f 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -83,6 +83,14 @@ class SchemaIndexesTests(TestCase): str(index.create_sql(Article, editor)), ) + def test_descending_columns_list_sql(self): + index = Index(fields=['-headline'], name='whitespace_idx') + editor = connection.schema_editor() + self.assertIn( + '(%s DESC)' % editor.quote_name('headline'), + str(index.create_sql(Article, editor)), + ) + @skipIf(connection.vendor == 'postgresql', 'opclasses are PostgreSQL only') class SchemaIndexesNotPostgreSQLTests(TransactionTestCase): @@ -231,6 +239,18 @@ class SchemaIndexesPostgreSQLTests(TransactionTestCase): cursor.execute(self.get_opclass_query % indexname) self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', indexname)]) + def test_ops_class_columns_lists_sql(self): + index = Index( + fields=['headline'], + name='whitespace_idx', + opclasses=['text_pattern_ops'], + ) + with connection.schema_editor() as editor: + self.assertIn( + '(%s text_pattern_ops)' % editor.quote_name('headline'), + str(index.create_sql(Article, editor)), + ) + def test_ops_class_descending_columns_list_sql(self): index = Index( fields=['-headline'],