Ticket #2061: pgsql_introspection_droppedcolumns.diff
File pgsql_introspection_droppedcolumns.diff, 2.0 KB (added by , 18 years ago) |
---|
-
django/db/backends/postgresql/introspection.py
45 45 {'primary_key': boolean representing whether it's the primary key, 46 46 'unique': boolean representing whether it's a unique index} 47 47 """ 48 # Get the table description because we only have the column indexes, and we 49 # need the column names. 50 desc = get_table_description(cursor, table_name) 51 # This query retrieves each index on the given table. 48 # This query retrieves each index on the given table, including the 49 # first associated field name 52 50 cursor.execute(""" 53 SELECT idx.indkey, idx.indisunique, idx.indisprimary51 SELECT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary 54 52 FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, 55 pg_catalog.pg_index idx 53 pg_catalog.pg_index idx, pg_catalog.pg_attribute attr 56 54 WHERE c.oid = idx.indrelid 57 55 AND idx.indexrelid = c2.oid 56 AND attr.attrelid = c.oid 57 AND attr.attnum = idx.indkey[0] 58 58 AND c.relname = %s""", [table_name]) 59 59 indexes = {} 60 60 for row in cursor.fetchall(): 61 # row[ 0] (idx.indkey) is stored in the DB as an array. It comes out as61 # row[1] (idx.indkey) is stored in the DB as an array. It comes out as 62 62 # a string of space-separated integers. This designates the field 63 63 # indexes (1-based) of the fields that have indexes on the table. 64 64 # Here, we skip any indexes across multiple fields. 65 if ' ' in row[ 0]:65 if ' ' in row[1]: 66 66 continue 67 col_name = desc[int(row[0])-1][0] 68 indexes[col_name] = {'primary_key': row[2], 'unique': row[1]} 67 indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]} 69 68 return indexes 70 69 71 70 # Maps type codes to Django Field types.