diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index ca389b9..90304aa 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
41 | 41 | return '' |
42 | 42 | |
43 | 43 | def sql_indexes_for_field(self, model, f, style): |
44 | | if f.db_index and not f.unique: |
| 44 | output = [] |
| 45 | if f.db_index: |
45 | 46 | qn = self.connection.ops.quote_name |
46 | 47 | db_table = model._meta.db_table |
47 | 48 | tablespace = f.db_tablespace or model._meta.db_tablespace |
… |
… |
class DatabaseCreation(BaseDatabaseCreation):
|
60 | 61 | "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) + |
61 | 62 | "%s;" % tablespace_sql) |
62 | 63 | |
63 | | output = [get_index_sql('%s_%s' % (db_table, f.column))] |
| 64 | if not f.unique: |
| 65 | output = [get_index_sql('%s_%s' % (db_table, f.column))] |
64 | 66 | |
65 | 67 | # Fields with database column types of `varchar` and `text` need |
66 | 68 | # a second index that specifies their operator class, which is |
… |
… |
class DatabaseCreation(BaseDatabaseCreation):
|
73 | 75 | elif db_type.startswith('text'): |
74 | 76 | output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
75 | 77 | ' text_pattern_ops')) |
76 | | else: |
77 | | output = [] |
78 | 78 | return output |
79 | 79 | |
80 | 80 | def set_autocommit(self): |
diff --git a/tests/regressiontests/indexes/models.py b/tests/regressiontests/indexes/models.py
index 9758377..47ba589 100644
a
|
b
|
class Article(models.Model):
|
9 | 9 | index_together = [ |
10 | 10 | ["headline", "pub_date"], |
11 | 11 | ] |
| 12 | |
| 13 | |
| 14 | class IndexedArticle(models.Model): |
| 15 | headline = models.CharField(max_length=100, db_index=True) |
| 16 | body = models.TextField(db_index=True) |
| 17 | slug = models.CharField(max_length=40, unique=True, db_index=True) |
diff --git a/tests/regressiontests/indexes/tests.py b/tests/regressiontests/indexes/tests.py
index 0dac881..f3a32a4 100644
a
|
b
|
|
1 | 1 | from django.core.management.color import no_style |
2 | 2 | from django.db import connections, DEFAULT_DB_ALIAS |
3 | 3 | from django.test import TestCase |
| 4 | from django.utils.unittest import skipUnless |
4 | 5 | |
5 | | from .models import Article |
| 6 | from .models import Article, IndexedArticle |
6 | 7 | |
7 | 8 | |
8 | 9 | class IndexesTests(TestCase): |
… |
… |
class IndexesTests(TestCase):
|
10 | 11 | connection = connections[DEFAULT_DB_ALIAS] |
11 | 12 | index_sql = connection.creation.sql_indexes_for_model(Article, no_style()) |
12 | 13 | self.assertEqual(len(index_sql), 1) |
| 14 | |
| 15 | @skipUnless(connections[DEFAULT_DB_ALIAS].vendor == 'postgresql', |
| 16 | "This is a postgresql-specific issue") |
| 17 | def test_postgresql_text_indexes(self): |
| 18 | """Test creation of PostgreSQL-specific text indexes (#12234)""" |
| 19 | connection = connections[DEFAULT_DB_ALIAS] |
| 20 | index_sql = connection.creation.sql_indexes_for_model(IndexedArticle, no_style()) |
| 21 | self.assertEqual(len(index_sql), 5) |
| 22 | self.assertIn('("headline" varchar_pattern_ops)', index_sql[1]) |
| 23 | self.assertIn('("body" text_pattern_ops)', index_sql[3]) |
| 24 | # unique=True and db_index=True should only create the varchar-specific |
| 25 | # index (#19441). |
| 26 | self.assertIn('("slug" varchar_pattern_ops)', index_sql[4]) |