summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2009-12-19 08:24:20 +0000
committerJustin Bronn <jbronn@gmail.com>2009-12-19 08:24:20 +0000
commit281114d2095608c93a00df2fc55a72f7cd5c2fcf (patch)
treef6dd7320c3e5e27fc3606fb9075d311b90cff907 /django
parentd10f9766ad76a16e32d5d7a28b0dd675b81a218f (diff)
[1.1.X] Fixed #12234 -- Create additional indexes that use the appropriate operation class for PostgreSQL `varchar` and `text` columns when `db_index=True`.
Backport of r11912 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11913 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/db/backends/postgresql/creation.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py
index 8f329feca4..f4f533ae1a 100644
--- a/django/db/backends/postgresql/creation.py
+++ b/django/db/backends/postgresql/creation.py
@@ -34,3 +34,42 @@ class DatabaseCreation(BaseDatabaseCreation):
if settings.TEST_DATABASE_CHARSET:
return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
return ''
+
+ def sql_indexes_for_field(self, model, f, style):
+ if f.db_index and not f.unique:
+ qn = self.connection.ops.quote_name
+ db_table = model._meta.db_table
+ tablespace = f.db_tablespace or model._meta.db_tablespace
+ if tablespace:
+ sql = self.connection.ops.tablespace_sql(tablespace)
+ if sql:
+ tablespace_sql = ' ' + sql
+ else:
+ tablespace_sql = ''
+ else:
+ tablespace_sql = ''
+
+ def get_index_sql(index_name, opclass=''):
+ return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+ style.SQL_TABLE(qn(index_name)) + ' ' +
+ style.SQL_KEYWORD('ON') + ' ' +
+ style.SQL_TABLE(qn(db_table)) + ' ' +
+ "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
+ "%s;" % tablespace_sql)
+
+ output = [get_index_sql('%s_%s' % (db_table, f.column))]
+
+ # Fields with database column types of `varchar` and `text` need
+ # a second index that specifies their operator class, which is
+ # needed when performing correct LIKE queries outside the
+ # C locale. See #12234.
+ db_type = f.db_type()
+ if db_type.startswith('varchar'):
+ output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
+ ' varchar_pattern_ops'))
+ elif db_type.startswith('text'):
+ output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
+ ' text_pattern_ops'))
+ else:
+ output = []
+ return output