Reflection of indexes in PostgreSQL doesn't preserve the order of columns

Issue #2767 resolved
Roman Podoliaka created an issue

Reflection of indexes in PostgreSQL returns columns in different order comparing to the one of original index. As order of columns in index definition does matter, this must be fixed.

Code to reproduce the bug:

import sqlalchemy as sa


eng = sa.create_engine('postgresql://test:test@localhost/test')
meta = sa.MetaData(bind=eng)
meta2 = sa.MetaData(bind=eng)

table = sa.Table(
    'testtbl', meta,
    sa.Column('a', sa.Integer),
    sa.Column('b', sa.Integer),
    sa.Column('c', sa.Integer),

    sa.Index('test_idx', 'b', 'a', 'c')
)
meta.create_all()

table2 = sa.Table('testtbl', meta2, autoload=True)
idx = table2.indexes.pop()

assert idx.columns.keys() == ['a', 'c']('b',)

I'll provide the fix soon via pull request on GitHub

Comments (2)

  1. Log in to comment