Ticket #19441: 19441-1.diff

File 19441-1.diff, 3.5 KB (added by Claude Paroz, 12 years ago)
  • django/db/backends/postgresql_psycopg2/creation.py

    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):  
    4141        return ''
    4242
    4343    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:
    4546            qn = self.connection.ops.quote_name
    4647            db_table = model._meta.db_table
    4748            tablespace = f.db_tablespace or model._meta.db_tablespace
    class DatabaseCreation(BaseDatabaseCreation):  
    6061                        "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
    6162                        "%s;" % tablespace_sql)
    6263
    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))]
    6466
    6567            # Fields with database column types of `varchar` and `text` need
    6668            # a second index that specifies their operator class, which is
    class DatabaseCreation(BaseDatabaseCreation):  
    7375            elif db_type.startswith('text'):
    7476                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
    7577                                            ' text_pattern_ops'))
    76         else:
    77             output = []
    7878        return output
    7979
    8080    def set_autocommit(self):
  • tests/regressiontests/indexes/models.py

    diff --git a/tests/regressiontests/indexes/models.py b/tests/regressiontests/indexes/models.py
    index 9758377..47ba589 100644
    a b class Article(models.Model):  
    99        index_together = [
    1010            ["headline", "pub_date"],
    1111        ]
     12
     13
     14class 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)
  • tests/regressiontests/indexes/tests.py

    diff --git a/tests/regressiontests/indexes/tests.py b/tests/regressiontests/indexes/tests.py
    index 0dac881..f3a32a4 100644
    a b  
    11from django.core.management.color import no_style
    22from django.db import connections, DEFAULT_DB_ALIAS
    33from django.test import TestCase
     4from django.utils.unittest import skipUnless
    45
    5 from .models import Article
     6from .models import Article, IndexedArticle
    67
    78
    89class IndexesTests(TestCase):
    class IndexesTests(TestCase):  
    1011        connection = connections[DEFAULT_DB_ALIAS]
    1112        index_sql = connection.creation.sql_indexes_for_model(Article, no_style())
    1213        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])
Back to Top