1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #2061 -- Fixed PostgreSQL index introspection in tables that have dropped columns. Thanks, Chris Chamberlin

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3047 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-06-01 16:27:41 +00:00
parent 8cd32bddab
commit fa9722489b
3 changed files with 19 additions and 20 deletions

View File

@ -45,6 +45,7 @@ answer newbie questions, and generally made Django that much better:
andy@jadedplanet.net andy@jadedplanet.net
Antonio Cavedoni <http://cavedoni.com/> Antonio Cavedoni <http://cavedoni.com/>
C8E C8E
Chris Chamberlin <dja@cdc.msbx.net>
Amit Chakradeo <http://amit.chakradeo.net/> Amit Chakradeo <http://amit.chakradeo.net/>
ChaosKCW ChaosKCW
Ian Clelland <clelland@gmail.com> Ian Clelland <clelland@gmail.com>

View File

@ -45,27 +45,26 @@ def get_indexes(cursor, table_name):
{'primary_key': boolean representing whether it's the primary key, {'primary_key': boolean representing whether it's the primary key,
'unique': boolean representing whether it's a unique index} 'unique': boolean representing whether it's a unique index}
""" """
# Get the table description because we only have the column indexes, and we # This query retrieves each index on the given table, including the
# need the column names. # first associated field name
desc = get_table_description(cursor, table_name)
# This query retrieves each index on the given table.
cursor.execute(""" cursor.execute("""
SELECT idx.indkey, idx.indisunique, idx.indisprimary SELECT attr.attname, idx.indkey, 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_index idx, pg_catalog.pg_attribute attr
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""", [table_name]) AND c.relname = %s""", [table_name])
indexes = {} indexes = {}
for row in cursor.fetchall(): for row in cursor.fetchall():
# row[0] (idx.indkey) is stored in the DB as an array. It comes out as # row[1] (idx.indkey) is stored in the DB as an array. It comes out as
# a string of space-separated integers. This designates the field # a string of space-separated integers. This designates the field
# indexes (1-based) of the fields that have indexes on the table. # indexes (1-based) of the fields that have indexes on the table.
# Here, we skip any indexes across multiple fields. # Here, we skip any indexes across multiple fields.
if ' ' in row[0]: if ' ' in row[1]:
continue continue
col_name = desc[int(row[0])-1][0] indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]}
indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
return indexes return indexes
# Maps type codes to Django Field types. # Maps type codes to Django Field types.

View File

@ -45,27 +45,26 @@ def get_indexes(cursor, table_name):
{'primary_key': boolean representing whether it's the primary key, {'primary_key': boolean representing whether it's the primary key,
'unique': boolean representing whether it's a unique index} 'unique': boolean representing whether it's a unique index}
""" """
# Get the table description because we only have the column indexes, and we # This query retrieves each index on the given table, including the
# need the column names. # first associated field name
desc = get_table_description(cursor, table_name)
# This query retrieves each index on the given table.
cursor.execute(""" cursor.execute("""
SELECT idx.indkey, idx.indisunique, idx.indisprimary SELECT attr.attname, idx.indkey, 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_index idx, pg_catalog.pg_attribute attr
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""", [table_name]) AND c.relname = %s""", [table_name])
indexes = {} indexes = {}
for row in cursor.fetchall(): for row in cursor.fetchall():
# row[0] (idx.indkey) is stored in the DB as an array. It comes out as # row[1] (idx.indkey) is stored in the DB as an array. It comes out as
# a string of space-separated integers. This designates the field # a string of space-separated integers. This designates the field
# indexes (1-based) of the fields that have indexes on the table. # indexes (1-based) of the fields that have indexes on the table.
# Here, we skip any indexes across multiple fields. # Here, we skip any indexes across multiple fields.
if ' ' in row[0]: if ' ' in row[1]:
continue continue
col_name = desc[int(row[0])-1][0] indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]}
indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
return indexes return indexes
# Maps type codes to Django Field types. # Maps type codes to Django Field types.