Ticket #12234: charfield_index_opclass_fix_v3.diff

File charfield_index_opclass_fix_v3.diff, 3.3 KB (added by jbronn, 14 years ago)

Introspect on database type string rather than Field class; added docs to databases section.

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

     
    3535        if settings.TEST_DATABASE_CHARSET:
    3636            return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
    3737        return ''
     38
     39    def sql_indexes_for_field(self, model, f, style):
     40        if f.db_index and not f.unique:
     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            # Fields with database column types of `varchar` and `text` need
     64            # a second index that specifies their operator class, which is
     65            # needed for performing LIKE queries specific that will work
     66            # outside the C locale. See #12234.
     67            db_type = f.db_type()
     68            if db_type.startswith('varchar'):
     69                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
     70                                            ' varchar_pattern_ops'))
     71            elif db_type.startswith('text'):
     72                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
     73                                            ' text_pattern_ops'))
     74        else:
     75            output = []
     76        return output
  • docs/ref/databases.txt

     
    8080before enabling this feature. It's faster, but it provides less automatic
    8181protection for multi-call operations.
    8282
     83Indexes for ``varchar`` and ``text`` columns
     84~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     85.. versionadded:: 1.1.2
     86
     87When specifying ``db_index=True`` on your model fields, Django typically
     88outputs a single ``CREATE INDEX`` statement.  However, if the database type
     89for the field is either ``varchar`` or ``text`` (e.g., used by ``CharField``,
     90``FileField``, and ``TextField``), then Django will create
     91an additional index that uses an appropriate `PostgreSQL operator class`_
     92for the column.  The extra index is necessary to correctly perfrom
     93lookups that use the ``LIKE`` operator in their SQL, as is done with the
     94``contains`` and ``startswith`` lookup types.
     95
     96.. _PostgreSQL operator class: http://www.postgresql.org/docs/8.4/static/indexes-opclass.html
     97
    8398.. _mysql-notes:
    8499
    85100MySQL notes
Back to Top