Code

Ticket #19441: 19441-1.diff

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