diff options
author | Akshesh <aksheshdoshi@gmail.com> | 2016-07-26 06:34:28 +0530 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2016-08-12 11:51:09 -0400 |
commit | f842d1011c1195aa26071a6ab6f96e0b8d907343 (patch) | |
tree | 18431668220d89053fe169f736f872f7fedd57ca /django/db/backends/postgresql/introspection.py | |
parent | 5eab1f6f8348497e87c19112786efb970e41f36e (diff) | |
download | django-f842d1011c1195aa26071a6ab6f96e0b8d907343.tar.gz |
Refs #20888 -- Added index order introspection.
Diffstat (limited to 'django/db/backends/postgresql/introspection.py')
-rw-r--r-- | django/db/backends/postgresql/introspection.py | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/django/db/backends/postgresql/introspection.py b/django/db/backends/postgresql/introspection.py index aa37c8d01e..85ea66f6d1 100644 --- a/django/db/backends/postgresql/introspection.py +++ b/django/db/backends/postgresql/introspection.py @@ -211,23 +211,36 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): # Now get indexes cursor.execute(""" SELECT - c2.relname, - ARRAY( - SELECT (SELECT attname FROM pg_catalog.pg_attribute WHERE attnum = i AND attrelid = c.oid) - FROM unnest(idx.indkey) i - ), - idx.indisunique, - idx.indisprimary - FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, - pg_catalog.pg_index idx - WHERE c.oid = idx.indrelid - AND idx.indexrelid = c2.oid - AND c.relname = %s + indexname, array_agg(attname), indisunique, indisprimary, + array_agg(ordering) + FROM ( + SELECT + c2.relname as indexname, idx.*, attr.attname, + CASE + WHEN am.amcanorder THEN + CASE (option & 1) + WHEN 1 THEN 'DESC' ELSE 'ASC' + END + END as ordering + FROM ( + SELECT + *, unnest(i.indkey) as key, unnest(i.indoption) as option + FROM pg_index i + ) idx, pg_class c, pg_class c2, pg_am am, pg_attribute attr + WHERE c.oid=idx.indrelid + AND idx.indexrelid=c2.oid + AND attr.attrelid=c.oid + AND attr.attnum=idx.key + AND c2.relam=am.oid + AND c.relname = %s + ) s2 + GROUP BY indexname, indisunique, indisprimary; """, [table_name]) - for index, columns, unique, primary in cursor.fetchall(): + for index, columns, unique, primary, orders in cursor.fetchall(): if index not in constraints: constraints[index] = { - "columns": list(columns), + "columns": columns, + "orders": orders, "primary_key": primary, "unique": unique, "foreign_key": None, |