Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19441 -- Created PostgreSQL varchar index when unique=…

…True

Thanks Dylan Verheul for the report and Anssi Kääriäinen for the
review.
Backport of 55972ee from master.
  • Loading branch information...
commit ddea0e0d7ee1a885f8ecbe45d7306a39c7780d63 1 parent 1caa483
Claude Paroz authored
8  django/db/backends/postgresql_psycopg2/creation.py
@@ -41,7 +41,8 @@ def sql_table_creation_suffix(self):
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
@@ -60,7 +61,8 @@ def get_index_sql(index_name, opclass=''):
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
@@ -73,8 +75,6 @@ def get_index_sql(index_name, opclass=''):
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):
6  tests/regressiontests/indexes/models.py
@@ -9,3 +9,9 @@ class Meta:
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)
16  tests/regressiontests/indexes/tests.py
... ...
@@ -1,8 +1,9 @@
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):
@@ -10,3 +11,16 @@ def test_index_together(self):
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])

0 notes on commit ddea0e0

Please sign in to comment.
Something went wrong with that request. Please try again.