Ticket #12234: charfield_index_opclass_fix_v2.diff

File charfield_index_opclass_fix_v2.diff, 2.1 KB (added by jbronn, 14 years ago)

simplifed get_index_sql

  • django/db/backends/postgresql/creation.py

     
    3434        if settings.TEST_DATABASE_CHARSET:
    3535            return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
    3636        return ''
     37
     38    def sql_indexes_for_field(self, model, f, style):
     39        if f.db_index and not f.unique:
     40            from django.db.models.fields import CharField, TextField
     41            qn = self.connection.ops.quote_name
     42            db_table = model._meta.db_table
     43            tablespace = f.db_tablespace or model._meta.db_tablespace
     44            if tablespace:
     45                sql = self.connection.ops.tablespace_sql(tablespace)
     46                if sql:
     47                    tablespace_sql = ' ' + sql
     48                else:
     49                    tablespace_sql = ''
     50            else:
     51                tablespace_sql = ''
     52
     53            def get_index_sql(index_name, opclass=''):
     54                return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
     55                        style.SQL_TABLE(qn(index_name)) + ' ' +
     56                        style.SQL_KEYWORD('ON') + ' ' +
     57                        style.SQL_TABLE(qn(db_table)) + ' ' +
     58                        "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
     59                        "%s;" % tablespace_sql)
     60
     61            output = [get_index_sql('%s_%s' % (db_table, f.column))]
     62
     63            # CharFields and TextFields require a second index that specifies
     64            # their operator class for performing LIKE queries. See #12234.
     65            if isinstance(f, CharField):
     66                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
     67                                            ' varchar_pattern_ops'))
     68            elif isinstance(f, TextField):
     69                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
     70                                            ' text_pattern_ops'))
     71        else:
     72            output = []
     73        return output
Back to Top