[Django] #29834: Union queryset with ordering breaks on ordering with derived querysets

25 views
Skip to first unread message

Django

unread,
Oct 9, 2018, 10:47:56 AM10/9/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei | Owner: nobody
Maertens |
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) | Keywords: orm, union,
Severity: Normal | ordering
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
May be related to #29692

Simple reproduction (the exact models are not relevant I think):

{{{#!python
>>> Dimension.objects.values_list('id', flat=True)
<QuerySet [10, 11, 12, 13, 14, 15, 16, 17, 18]>

>>> qs = (
Dimension.objects.filter(pk__in=[10, 11])
.union(Dimension.objects.filter(pk__in=[16, 17])
.order_by('order')
)

>>> qs
<QuerySet [<Dimension: boeksoort>, <Dimension: grootboek>, <Dimension:
kenteken>, <Dimension: activa>]>

# this causes re-evaluation of the original qs to break
>>> qs.order_by().values_list('pk', flat=True)
<QuerySet [16, 11, 10, 17]>

>>> qs
[breaks]
}}}

Traceback:
{{{
Traceback (most recent call last):
File "<input>", line 1, in <module>
qs
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/mod
els/query.py", line 248, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/mod
els/query.py", line 272, in __iter__
self._fetch_all()
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/mod
els/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/mod
els/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
chunk_size=
self.chunk_size)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/mod
els/sql/compiler.py", line 1068, in execute_sql
cursor.execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/bac
kends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/bac
kends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False,
executor=self._e
xecute)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/bac
kends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/bac
kends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/uti
ls.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/bac
kends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ORDER BY position 4 is not in select
list
LINE 1: ...dimensions_dimension"."id" IN (16, 17)) ORDER BY (4) ASC LIM...
^
}}}

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

Django

unread,
Oct 9, 2018, 10:50:32 AM10/9/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage:
ordering | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sergei Maertens:

Old description:

New description:

>>> qs
[breaks]
}}}

Evaluating the qs instead of creating a new qs makes the code proceed as
expected.

{{{#!python
[dim.id for dim in qs]
}}}

--

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

Django

unread,
Oct 9, 2018, 11:29:11 AM10/9/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage:
ordering | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sergei Maertens:

Old description:

> May be related to #29692

> Evaluating the qs instead of creating a new qs makes the code proceed as
> expected.
>
> {{{#!python
> [dim.id for dim in qs]
> }}}

New description:

May be related to #29692

Simple reproduction (the exact models are not relevant I think):

{{{#!python
>>> Dimension.objects.values_list('id', flat=True)
<QuerySet [10, 11, 12, 13, 14, 15, 16, 17, 18]>

>>> qs = (
Dimension.objects.filter(pk__in=[10, 11])
.union(Dimension.objects.filter(pk__in=[16, 17])
.order_by('order')
)

>>> qs
<QuerySet [<Dimension: boeksoort>, <Dimension: grootboek>, <Dimension:
kenteken>, <Dimension: activa>]>

# this causes re-evaluation of the original qs to break
>>> qs.order_by().values_list('pk', flat=True)
<QuerySet [16, 11, 10, 17]>

>>> qs
[breaks]
}}}

Traceback:
{{{
Traceback (most recent call last):
File "<input>", line 1, in <module>
qs
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/models/query.py", line 248, in __repr__


data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/models/query.py", line 272, in __iter__
self._fetch_all()
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/models/query.py", line 1179, in _fetch_all


self._result_cache = list(self._iterable_class(self))
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/models/query.py", line 53, in __iter__


results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
chunk_size=self.chunk_size)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/models/sql/compiler.py", line 1068, in execute_sql
cursor.execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-
packages/django/db/backends/utils.py", line 100, in execute


return super().execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/backends/utils.py", line 68, in execute


return self._execute_with_wrappers(sql, params, many=False,
executor=self._execute)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers


return executor(sql, params, many, context)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/backends/utils.py", line 85, in _execute


return self.cursor.execute(sql, params)
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/utils.py", line 89, in __exit__


raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/bbt/.virtualenvs/ispnext/lib/python3.6/site-

packages/django/db/backends/utils.py", line 85, in _execute


return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ORDER BY position 4 is not in select
list
LINE 1: ...dimensions_dimension"."id" IN (16, 17)) ORDER BY (4) ASC LIM...
^
}}}

Evaluating the qs instead of creating a new qs makes the code proceed as
expected.

{{{#!python
[dim.id for dim in qs]
}}}

--

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

Django

unread,
Oct 9, 2018, 12:44:17 PM10/9/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage:
ordering | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: felixxm (added)


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

Django

unread,
Oct 9, 2018, 1:56:48 PM10/9/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage:
ordering | Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Simon Charette):

Looks like a bug caused by a `.query` attribute change without performing
a prior `copy()` of the query/queryset.

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

Django

unread,
Oct 10, 2018, 11:26:26 AM10/10/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 0 | Needs documentation: 0

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

* stage: Unreviewed => Accepted


Comment:

Attaching a regression test that fails on master (tested at
f3d3338e06d571a529bb2046428eeac8e56bcbf6).

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

Django

unread,
Oct 10, 2018, 11:26:42 AM10/10/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 0 | Needs documentation: 0

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

* Attachment "t29834.diff" added.

Django

unread,
Oct 24, 2018, 9:58:49 AM10/24/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* status: new => assigned
* owner: nobody => Can Sarıgöl


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

Django

unread,
Oct 24, 2018, 10:27:48 AM10/24/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* has_patch: 0 => 1


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:7>

Django

unread,
Nov 1, 2018, 7:43:30 PM11/1/18
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1


Comment:

Tests aren't passing.

--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:8>

Django

unread,
Feb 18, 2019, 8:34:13 AM2/18/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:9>

Django

unread,
Mar 6, 2019, 6:00:25 AM3/6/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1

* needs_tests: 0 => 1


Comment:

Needs more tests, at the least. But discussion on PR suggests we've not
hit the right strategy yet.

--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:10>

Django

unread,
Apr 6, 2019, 1:32:26 PM4/6/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0

* version: 2.0 => master
* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:11>

Django

unread,
Jun 7, 2019, 1:56:33 PM6/7/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1


Comment:

Alternate approach suggested on PR to be considered.

--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:12>

Django

unread,
Jun 11, 2019, 4:31:09 AM6/11/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:13>

Django

unread,
Jun 17, 2019, 8:32:51 AM6/17/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:14>

Django

unread,
Jun 19, 2019, 4:25:57 AM6/19/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:15>

Django

unread,
Jun 19, 2019, 6:28:45 AM6/19/19
to django-...@googlegroups.com
#29834: Union queryset with ordering breaks on ordering with derived querysets
-------------------------------------+-------------------------------------
Reporter: Sergei Maertens | Owner: Can
| Sarıgöl
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: orm, union, | Triage Stage: Accepted
ordering |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"2cbd3967e0a51eab993df89679046d25ec78baec" 2cbd3967]:
{{{
#!CommitTicketReference repository=""
revision="2cbd3967e0a51eab993df89679046d25ec78baec"
Fixed #29834 -- Fixed column mismatch crash with
QuerySet.values()/values_list() and order_by() on combined querysets.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29834#comment:16>

Reply all
Reply to author
Forward
0 new messages