[Django] #28197: db.backends.postgresql.introspection.get_constraints retrieves index fields in arbitrary order

4 views
Skip to first unread message

Django

unread,
May 12, 2017, 11:59:34 AM5/12/17
to django-...@googlegroups.com
#28197: db.backends.postgresql.introspection.get_constraints retrieves index fields
in arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When trying to run the migrations from scratch for the django-blog-zinna
app the first time, the migration command threw the following error when
run under django 1.11 on a postgresql db:
`ValueError: Found wrong number (0) of constraints for zinnia_entry(slug,
creation_date)`

After digging through the code, I found out, that the `array_agg` function
used in the query at `db.backends.postgresql.introspection` line 196
aggregates the values in an arbitrary order.

--
Ticket URL: <https://code.djangoproject.com/ticket/28197>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 12, 2017, 12:04:52 PM5/12/17
to django-...@googlegroups.com
#28197: db.backends.postgresql.introspection.get_constraints retrieves index fields
in arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mactory):

I have submitted a pull-request that adds a row number to the subquery
which can then be used to order the field names in the correct order. Not
sure if this is the best way to solve the problem, but it seems to be the
one with the least changes necessary.

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:1>

Django

unread,
May 12, 2017, 2:04:27 PM5/12/17
to django-...@googlegroups.com
#28197: PostgreSQL introspection's get_constraints() retrieves index fields in

arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1
* severity: Normal => Release blocker
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Looks like a regression in f842d1011c1195aa26071a6ab6f96e0b8d907343. Can
you add a regression test to the PR?

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:2>

Django

unread,
May 12, 2017, 2:07:02 PM5/12/17
to django-...@googlegroups.com
#28197: PostgreSQL introspection's get_constraints() retrieves index fields in

arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

From the PostgreSQL docs:
The aggregate functions array_agg, json_agg, json_object_agg,
string_agg, and xmlagg, as well as similar user-defined aggregate
functions, produce meaningfully different result values depending on the
order of the input values. This ordering is unspecified by default, but
can be controlled by writing an ORDER BY clause within the aggregate call,
as shown in Section 4.2.7. Alternatively, supplying the input values from
a sorted subquery will usually work.`

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:3>

Django

unread,
May 14, 2017, 1:39:58 PM5/14/17
to django-...@googlegroups.com
#28197: PostgreSQL introspection's get_constraints() retrieves index fields in

arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mactory):

* needs_tests: 1 => 0


Comment:

I have extended the existing tests to also test a 4 field index. The test
fails on my machine without the patch and passes with the patch.
Since the ordering of the fields was, as far as I could see it, arbitrary,
it could happen though, that the problem exists but is not detected by the
test. This can be if the ordering just happens to be correct. I don't see
any possibility though to catch randomly correct orderings in a test. If
you know any techniques to do so, please feel free to extend the tests
further.

[https://github.com/django/django/pull/8495 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:4>

Django

unread,
May 15, 2017, 8:59:04 AM5/15/17
to django-...@googlegroups.com
#28197: PostgreSQL introspection's get_constraints() retrieves index fields in

arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"3a5299c19cd5a38f7fa0f45ed2df7b10f0c9cf5d" 3a5299c]:
{{{
#!CommitTicketReference repository=""
revision="3a5299c19cd5a38f7fa0f45ed2df7b10f0c9cf5d"
Fixed #28197 -- Fixed introspection of index field ordering on PostgreSQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:5>

Django

unread,
May 15, 2017, 9:02:43 AM5/15/17
to django-...@googlegroups.com
#28197: PostgreSQL introspection's get_constraints() retrieves index fields in

arbitrary order
-------------------------------------+-------------------------------------
Reporter: Mactory | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"566726ff964b004f2a6adecc1e51197bc34d78d2" 566726ff]:
{{{
#!CommitTicketReference repository=""
revision="566726ff964b004f2a6adecc1e51197bc34d78d2"
[1.11.x] Fixed #28197 -- Fixed introspection of index field ordering on
PostgreSQL.

Backport of 3a5299c19cd5a38f7fa0f45ed2df7b10f0c9cf5d from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28197#comment:6>

Reply all
Reply to author
Forward
0 new messages