{{{
# 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.
* 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>
* Attachment "25312-test.diff" added.
* cc: josh.smeaton@… (added)
Comment:
This looks related to #25307
--
Ticket URL: <https://code.djangoproject.com/ticket/25316#comment:2>
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>
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>
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>
* status: new => assigned
* owner: nobody => varunnaganathan
--
Ticket URL: <https://code.djangoproject.com/ticket/25316#comment:6>
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>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25316#comment:8>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25316#comment:9>
* 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>
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>
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>
* stage: Ready for checkin => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/25316#comment:13>