#34771: order_by on annotated field that's not present in values/values_list 
causes
SQL syntax error
-------------------------------------+-------------------------------------
     Reporter:  Yitao Xiong          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  mysql                |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

 * cc: Simon Charette (added)
 * keywords:  SQL syntax order_by annotate => mysql


Comment:

 Hello tinyx, thank you for your ticket.

 My first thought when reading the description is that "it makes no sense"
 to order a query using a field that was not included in `values` list,
 though I can't find docs that explicitly confirm my suspicion.

 I tested this in Postgresql (15.3) and obtained a successful query run:
 {{{

  >>> print(User.objects.annotate(random_stuff=Value(False,
 output_field=BooleanField())).values('id').order_by('random_stuff').query)
 SELECT "auth_user"."id" FROM "auth_user" ORDER BY (False)::boolean ASC
 >>> User.objects.annotate(random_stuff=Value(False,
 output_field=BooleanField())).values('id').order_by('random_stuff')
 <QuerySet [{'id': 2}, {'id': 3}, {'id': 1}, {'id': 4}, {'id': 5}, {'id':
 6}, {'id': 7}]>
 >>>
 }}}

 Nevertheless, I would like to understand your use case a little bit more.
 I can imagine you defined `random_stuff` purposely silly to simplify the
 example, but would you have a more concrete/real use case that you could
 share with us?

 Also, is there any chance that you can test this same example in Django
 4.2 (or even better, `main`)? I don't have a handy mysql to test myself at
 this time.

 Lastly, I'm cc'ing Simon who may have more specific thoughts on the
 matter.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070189e5843d58-efa92bc3-7e55-4afc-8df6-11bb1f595242-000000%40eu-central-1.amazonses.com.

Reply via email to