From 566726ff964b004f2a6adecc1e51197bc34d78d2 Mon Sep 17 00:00:00 2001 From: Tim Schneider Date: Fri, 12 May 2017 18:01:30 +0200 Subject: [PATCH] [1.11.x] Fixed #28197 -- Fixed introspection of index field ordering on PostgreSQL. Backport of 3a5299c19cd5a38f7fa0f45ed2df7b10f0c9cf5d from master --- django/db/backends/postgresql/introspection.py | 10 +++++++--- docs/releases/1.11.2.txt | 2 ++ tests/introspection/models.py | 1 + tests/introspection/tests.py | 7 ++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/django/db/backends/postgresql/introspection.py b/django/db/backends/postgresql/introspection.py index c75f06f873..032a777aa5 100644 --- a/django/db/backends/postgresql/introspection.py +++ b/django/db/backends/postgresql/introspection.py @@ -196,13 +196,17 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): "options": options, } # Now get indexes + # The row_number() function for ordering the index fields can be + # replaced by WITH ORDINALITY in the unnest() functions when support + # for PostgreSQL 9.3 is dropped. cursor.execute(""" SELECT - indexname, array_agg(attname), indisunique, indisprimary, - array_agg(ordering), amname, exprdef, s2.attoptions + indexname, array_agg(attname ORDER BY rnum), indisunique, indisprimary, + array_agg(ordering ORDER BY rnum), amname, exprdef, s2.attoptions FROM ( SELECT - c2.relname as indexname, idx.*, attr.attname, am.amname, + row_number() OVER () as rnum, c2.relname as indexname, + idx.*, attr.attname, am.amname, CASE WHEN idx.indexprs IS NOT NULL THEN pg_get_indexdef(idx.indexrelid) diff --git a/docs/releases/1.11.2.txt b/docs/releases/1.11.2.txt index 6831f60cef..8d82581966 100644 --- a/docs/releases/1.11.2.txt +++ b/docs/releases/1.11.2.txt @@ -23,3 +23,5 @@ Bugfixes * Fixed ``django.contrib.auth.authenticate()`` when multiple authentication backends don't accept a positional ``request`` argument (:ticket:`28207`). + +* Fixed introspection of index field ordering on PostgreSQL (:ticket:`28197`). diff --git a/tests/introspection/models.py b/tests/introspection/models.py index b8c7a83b9f..022895df52 100644 --- a/tests/introspection/models.py +++ b/tests/introspection/models.py @@ -54,6 +54,7 @@ class Article(models.Model): ordering = ('headline',) index_together = [ ["headline", "pub_date"], + ['headline', 'response_to', 'pub_date', 'reporter'], ] diff --git a/tests/introspection/tests.py b/tests/introspection/tests.py index 18283f6fbb..c4a2ef56fe 100644 --- a/tests/introspection/tests.py +++ b/tests/introspection/tests.py @@ -191,10 +191,14 @@ class IntrospectionTests(TransactionTestCase): with connection.cursor() as cursor: constraints = connection.introspection.get_constraints(cursor, Article._meta.db_table) index = {} + index2 = {} for key, val in constraints.items(): if val['columns'] == ['headline', 'pub_date']: index = val + if val['columns'] == ['headline', 'response_to_id', 'pub_date', 'reporter_id']: + index2 = val self.assertEqual(index['type'], Index.suffix) + self.assertEqual(index2['type'], Index.suffix) @skipUnlessDBFeature('supports_index_column_ordering') def test_get_constraints_indexes_orders(self): @@ -208,13 +212,14 @@ class IntrospectionTests(TransactionTestCase): ['reporter_id'], ['headline', 'pub_date'], ['response_to_id'], + ['headline', 'response_to_id', 'pub_date', 'reporter_id'], ] for key, val in constraints.items(): if val['index'] and not (val['primary_key'] or val['unique']): self.assertIn(val['columns'], expected_columns) self.assertEqual(val['orders'], ['ASC'] * len(val['columns'])) indexes_verified += 1 - self.assertEqual(indexes_verified, 3) + self.assertEqual(indexes_verified, 4) def datatype(dbtype, description):