Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2061 closed defect (fixed)

[patch] PostgreSQL index introspection fails on tables that have dropped columns

Reported by: Chris Chamberlin <dja@…> Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When building models from an existing PostgreSQL database, python inspectdb fails if the table being inspected has had columns that were created and then dropped before inspection.

Traceback (most recent call last):
  File "", line 11, in ?
  File "/usr/lib/python2.3/site-packages/django/core/", line 1250
, in execute_manager
  File "/usr/lib/python2.3/site-packages/django/core/", line 1179
, in execute_from_command_line
    for line in action_mapping[action]():
  File "/usr/lib/python2.3/site-packages/django/core/", line 716,
 in inspectdb
    indexes = introspection_module.get_indexes(cursor, table_name)
  File "/usr/lib/python2.3/site-packages/django/db/backends/postgresql/introsp", 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 "". 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/ should get the same patch.

Attachments (1)

pgsql_introspection_droppedcolumns.diff (2.0 KB) - added by Chris Chamberlin <dja@…> 8 years ago.
patch of django/db/backends/postgresql/

Download all attachments as: .zip

Change History (3)

Changed 8 years ago by Chris Chamberlin <dja@…>

patch of django/db/backends/postgresql/

comment:1 Changed 8 years ago by anonymous

  • Summary changed from PostgreSQL index introspection fails on tables that have dropped columns to [patch] PostgreSQL index introspection fails on tables that have dropped columns

comment:2 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [3047]) Fixed #2061 -- Fixed PostgreSQL index introspection in tables that have dropped columns. Thanks, Chris Chamberlin

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.