[1.11.x] Fixed #28197 -- Fixed introspection of index field ordering on PostgreSQL.

Backport of 3a5299c19c from master
This commit is contained in:
Tim Schneider 2017-05-12 18:01:30 +02:00 committed by Tim Graham
parent d945b7e42a
commit 566726ff96
4 changed files with 16 additions and 4 deletions

View File

@ -196,13 +196,17 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
"options": options, "options": options,
} }
# Now get indexes # 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(""" cursor.execute("""
SELECT SELECT
indexname, array_agg(attname), indisunique, indisprimary, indexname, array_agg(attname ORDER BY rnum), indisunique, indisprimary,
array_agg(ordering), amname, exprdef, s2.attoptions array_agg(ordering ORDER BY rnum), amname, exprdef, s2.attoptions
FROM ( FROM (
SELECT 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 CASE
WHEN idx.indexprs IS NOT NULL THEN WHEN idx.indexprs IS NOT NULL THEN
pg_get_indexdef(idx.indexrelid) pg_get_indexdef(idx.indexrelid)

View File

@ -23,3 +23,5 @@ Bugfixes
* Fixed ``django.contrib.auth.authenticate()`` when multiple authentication * Fixed ``django.contrib.auth.authenticate()`` when multiple authentication
backends don't accept a positional ``request`` argument (:ticket:`28207`). backends don't accept a positional ``request`` argument (:ticket:`28207`).
* Fixed introspection of index field ordering on PostgreSQL (:ticket:`28197`).

View File

@ -54,6 +54,7 @@ class Article(models.Model):
ordering = ('headline',) ordering = ('headline',)
index_together = [ index_together = [
["headline", "pub_date"], ["headline", "pub_date"],
['headline', 'response_to', 'pub_date', 'reporter'],
] ]

View File

@ -191,10 +191,14 @@ class IntrospectionTests(TransactionTestCase):
with connection.cursor() as cursor: with connection.cursor() as cursor:
constraints = connection.introspection.get_constraints(cursor, Article._meta.db_table) constraints = connection.introspection.get_constraints(cursor, Article._meta.db_table)
index = {} index = {}
index2 = {}
for key, val in constraints.items(): for key, val in constraints.items():
if val['columns'] == ['headline', 'pub_date']: if val['columns'] == ['headline', 'pub_date']:
index = val index = val
if val['columns'] == ['headline', 'response_to_id', 'pub_date', 'reporter_id']:
index2 = val
self.assertEqual(index['type'], Index.suffix) self.assertEqual(index['type'], Index.suffix)
self.assertEqual(index2['type'], Index.suffix)
@skipUnlessDBFeature('supports_index_column_ordering') @skipUnlessDBFeature('supports_index_column_ordering')
def test_get_constraints_indexes_orders(self): def test_get_constraints_indexes_orders(self):
@ -208,13 +212,14 @@ class IntrospectionTests(TransactionTestCase):
['reporter_id'], ['reporter_id'],
['headline', 'pub_date'], ['headline', 'pub_date'],
['response_to_id'], ['response_to_id'],
['headline', 'response_to_id', 'pub_date', 'reporter_id'],
] ]
for key, val in constraints.items(): for key, val in constraints.items():
if val['index'] and not (val['primary_key'] or val['unique']): if val['index'] and not (val['primary_key'] or val['unique']):
self.assertIn(val['columns'], expected_columns) self.assertIn(val['columns'], expected_columns)
self.assertEqual(val['orders'], ['ASC'] * len(val['columns'])) self.assertEqual(val['orders'], ['ASC'] * len(val['columns']))
indexes_verified += 1 indexes_verified += 1
self.assertEqual(indexes_verified, 3) self.assertEqual(indexes_verified, 4)
def datatype(dbtype, description): def datatype(dbtype, description):