When building models from an existing PostgreSQL database, python manage.py inspectdb
fails if the table being inspected has had columns that were created and then dropped before inspection.
Traceback (most recent call last):
File "manage.py", line 11, in ?
execute_manager(settings)
File "/usr/lib/python2.3/site-packages/django/core/management.py", line 1250
, in execute_manager
execute_from_command_line(action_mapping)
File "/usr/lib/python2.3/site-packages/django/core/management.py", line 1179
, in execute_from_command_line
for line in action_mapping[action]():
File "/usr/lib/python2.3/site-packages/django/core/management.py", line 716,
in inspectdb
indexes = introspection_module.get_indexes(cursor, table_name)
File "/usr/lib/python2.3/site-packages/django/db/backends/postgresql/introsp
ection.py", line 67, in get_indexes
col_name = desc[int(row[0])-1][0]
IndexError: tuple index out of range
The problem is that when a column is dropped, PostgreSQL doesn't actually remove it from the pg_catalog.pg_attribute table, which the pg_catalog.pg_index.indkey column references; instead, it just renames them to something like "...pg.dropped.4...". The pgsql driver then leaves that column out of its column list, but this means you can't index into an array of columns using the indkey field.
I'll attach a patch that fixes get_indexes() by joining in the indexed column names in the main query. This is tested with PostgreSQL 7.4.8; I don't have access to 8.x. Also, I assume django/db/backends/postgresql_psycopg2/introspection.py
should get the same patch.
patch of django/db/backends/postgresql/introspection.py