Fix get_constraints to do multi-column indexes properly on pg

This commit is contained in:
Andrew Godwin 2013-07-02 12:06:00 +01:00
parent 9ef715d256
commit dbd3e775c1
1 changed files with 16 additions and 11 deletions

View File

@ -177,26 +177,31 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
constraints[constraint]['columns'].add(column) constraints[constraint]['columns'].add(column)
# Now get indexes # Now get indexes
cursor.execute(""" 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, 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 WHERE c.oid = idx.indrelid
AND idx.indexrelid = c2.oid AND idx.indexrelid = c2.oid
AND attr.attrelid = c.oid
AND attr.attnum = idx.indkey[0]
AND c.relname = %s AND c.relname = %s
""", [table_name]) """, [table_name])
for index, column, coli, unique, primary in cursor.fetchall(): for index, columns, unique, primary in cursor.fetchall():
# If we're the first column, make the record
if index not in constraints: if index not in constraints:
constraints[index] = { constraints[index] = {
"columns": set(), "columns": set(columns),
"primary_key": False, "primary_key": primary,
"unique": False, "unique": unique,
"foreign_key": False, "foreign_key": False,
"check": False, "check": False,
"index": True, "index": True,
} }
# Record the details
constraints[index]['columns'].add(column)
return constraints return constraints