Project

General

Profile

« Previous | Next » 

Revision 866

sql.py: Fixed index_cols() to handle UNIQUE indexes with expressions, whose column names are stored in a different format

View differences:

lib/sql.py
173 173
    if module == 'psycopg2':
174 174
        return list(values(run_query(db, '''\
175 175
SELECT attname
176
FROM pg_index
177
JOIN pg_class index ON index.oid = indexrelid
178
JOIN pg_class table_ ON table_.oid = indrelid
179
JOIN pg_attribute ON attrelid = indrelid AND attnum = ANY (indkey)
180
WHERE
181
    table_.relname = %(table)s
182
    AND index.relname = %(index)s
176
FROM
177
(
178
        SELECT attnum, attname
179
        FROM pg_index
180
        JOIN pg_class index ON index.oid = indexrelid
181
        JOIN pg_class table_ ON table_.oid = indrelid
182
        JOIN pg_attribute ON attrelid = indrelid AND attnum = ANY (indkey)
183
        WHERE
184
            table_.relname = %(table)s
185
            AND index.relname = %(index)s
186
    UNION
187
        SELECT attnum, attname
188
        FROM
189
        (
190
            SELECT
191
                indrelid
192
                , (regexp_matches(indexprs, E':varattno (\\\\d+)', 'g'))[1]::int
193
                    AS indkey
194
            FROM pg_index
195
            JOIN pg_class index ON index.oid = indexrelid
196
            JOIN pg_class table_ ON table_.oid = indrelid
197
            WHERE
198
                table_.relname = %(table)s
199
                AND index.relname = %(index)s
200
        ) s
201
        JOIN pg_attribute ON attrelid = indrelid AND attnum = indkey
202
) s
183 203
ORDER BY attnum
184 204
''',
185 205
            {'table': table, 'index': index})))

Also available in: Unified diff