[Django] #25316: Can't use order_by and values(_list) together after annotating

59 views
Skip to first unread message

Django

unread,
Aug 26, 2015, 10:30:08 AM8/26/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
----------------------------------------------+----------------------------
Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords: annotate,
| order_by
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+----------------------------
Example:


{{{
# model

from django.db import models
class Table(models.Model):
value = models.IntegerField()

# tests

from django.test import TestCase
from testapp.models import Table
from django.db.models import IntegerField, Case, Value, When
class Test1(TestCase):
def test_1(self):
query = Table.objects.annotate(
other=Case(
When(value=11, then=Value(1)),
output_field=IntegerField()
),
).order_by('other', 'value').values('id')

print query

}}}

The error is:

{{{
AttributeError: 'WhereNode' object has no attribute 'resolve_expression'
}}}

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

Django

unread,
Aug 27, 2015, 10:36:36 AM8/27/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

Attached is a test to reproduce the crash (at
956df84a613d4b9a92c979e46557243d288282c8).

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

Django

unread,
Aug 27, 2015, 10:36:50 AM8/27/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* Attachment "25312-test.diff" added.

Django

unread,
Aug 27, 2015, 9:32:48 PM8/27/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* cc: josh.smeaton@… (added)


Comment:

This looks related to #25307

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

Django

unread,
Nov 7, 2015, 10:56:06 AM11/7/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by lucasmoeskops):

I think this is not an issue.

The problem is that the example is annotating a field and then sorts on
this field. To do this, the field needs to exist still in the select. It
is however thrown away by values, so it can't sort on it.

The example will work correctly if the annotated field is added to the
values list as well. It will also work correctly if no order on the
annotated field is applied.

If this were to work, the resulting query would be considerably more
complex, involving a different final select than the select to apply the
ordering on.

What could be improved though is the error message. This is clear when no
annotation is used. Maybe something like "Can't deselect annotated value"
or "Can't order on unselected annotated field `other`"?

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

Django

unread,
Nov 7, 2015, 11:11:24 AM11/7/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by akaariai):

We do add "unselected" fields to the end of select list for some cases
already. It is perhaps possible to do so here, too.

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

Django

unread,
Dec 21, 2015, 7:37:49 AM12/21/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------

Reporter: adrian17 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by varunnaganathan):

I don't think the problem is because the field is not available in the
select.
When I use an inbuilt function like "Count" to annotate instead of the
"When" and "then" clause,the error doesnt seem to appear.
If the field used to sort not being available in select was the
problem,even use of "annotate" with "Count" would give the same error.

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

Django

unread,
Dec 22, 2015, 10:09:29 AM12/22/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: assigned

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => assigned
* owner: nobody => varunnaganathan


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

Django

unread,
Dec 22, 2015, 10:10:27 AM12/22/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by varunnaganathan):

Please review PR
https://github.com/django/django/pull/5852
I'm new to open source contributing.Sorry if I wasn't supposed to self
assign the bug to me.

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

Django

unread,
Dec 29, 2015, 1:22:05 PM12/29/15
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* has_patch: 0 => 1


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

Django

unread,
Jan 2, 2016, 6:53:50 AM1/2/16
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: annotate, order_by | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* stage: Accepted => Ready for checkin


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

Django

unread,
Jan 2, 2016, 8:19:51 AM1/2/16
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: closed

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

Keywords: annotate, order_by | Triage Stage: Ready for
| checkin
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: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3eba9638ee69138c73efb1d1c1d1b806ddafc6cf" 3eba9638]:
{{{
#!CommitTicketReference repository=""
revision="3eba9638ee69138c73efb1d1c1d1b806ddafc6cf"
Fixed #25316 -- Fixed a crash with order_by() and values() after
annotate().
}}}

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

Django

unread,
Jan 2, 2016, 8:26:52 AM1/2/16
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: annotate, order_by | Triage Stage: Ready for
| checkin
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:"f6b4893a9fcc3f9888491820e31c02e074d86341" f6b4893]:
{{{
#!CommitTicketReference repository=""
revision="f6b4893a9fcc3f9888491820e31c02e074d86341"
[1.8.x] Fixed #25316 -- Fixed a crash with order_by() and values() after
annotate().

Backport of 3eba9638ee69138c73efb1d1c1d1b806ddafc6cf from master
}}}

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

Django

unread,
Jan 2, 2016, 8:26:53 AM1/2/16
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: adrian17 | Owner:
| varunnaganathan
Type: Bug | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: annotate, order_by | Triage Stage: Ready for
| checkin
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:"5770c2382a4ebeebaaf3c2736e3934a191265a18" 5770c23]:
{{{
#!CommitTicketReference repository=""
revision="5770c2382a4ebeebaaf3c2736e3934a191265a18"
[1.9.x] Fixed #25316 -- Fixed a crash with order_by() and values() after
annotate().

Backport of 3eba9638ee69138c73efb1d1c1d1b806ddafc6cf from master
}}}

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

Django

unread,
Oct 29, 2016, 7:24:46 PM10/29/16
to django-...@googlegroups.com
#25316: Can't use order_by and values(_list) together after annotating
-------------------------------------+-------------------------------------
Reporter: Adrian Wielgosik | Owner: varun

| naganathan
Type: Bug | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: annotate, order_by | 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):

* stage: Ready for checkin => Accepted


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

Reply all
Reply to author
Forward
0 new messages