Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix get_constraints to do multi-column indexes properly on pg

  • Loading branch information...
commit dbd3e775c1d2209d2b35cb1fa4b82e20b18c99d7 1 parent 9ef715d
@andrewgodwin andrewgodwin authored
Showing with 16 additions and 11 deletions.
  1. +16 −11 django/db/backends/postgresql_psycopg2/introspection.py
View
27 django/db/backends/postgresql_psycopg2/introspection.py
@@ -177,26 +177,31 @@ def get_constraints(self, cursor, table_name):
constraints[constraint]['columns'].add(column)
# Now get indexes
cursor.execute("""
- SELECT c2.relname, attr.attname, idx.indkey, idx.indisunique, idx.indisprimary
+ SELECT
+ c2.relname,
+ ARRAY(
+ SELECT attr.attname
+ FROM unnest(idx.indkey) i, pg_catalog.pg_attribute attr
+ WHERE
+ attr.attnum = i AND
+ attr.attrelid = c.oid
+ ),
+ idx.indisunique,
+ idx.indisprimary
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
- pg_catalog.pg_index idx, pg_catalog.pg_attribute attr
+ pg_catalog.pg_index idx
WHERE c.oid = idx.indrelid
AND idx.indexrelid = c2.oid
- AND attr.attrelid = c.oid
- AND attr.attnum = idx.indkey[0]
AND c.relname = %s
""", [table_name])
- for index, column, coli, unique, primary in cursor.fetchall():
- # If we're the first column, make the record
+ for index, columns, unique, primary in cursor.fetchall():
if index not in constraints:
constraints[index] = {
- "columns": set(),
- "primary_key": False,
- "unique": False,
+ "columns": set(columns),
+ "primary_key": primary,
+ "unique": unique,
"foreign_key": False,
"check": False,
"index": True,
}
- # Record the details
- constraints[index]['columns'].add(column)
return constraints
Please sign in to comment.
Something went wrong with that request. Please try again.