#32658: Subquery ignores query ordering
-------------------------------------+-------------------------------------
     Reporter:  Bálint Balina        |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  closed
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  Subquery,            |             Triage Stage:
  annotations, ordering              |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * cc: Simon Charette (added)
 * status:  new => closed
 * resolution:   => invalid


Comment:

 IIRC the ordering is cleared for optimization purposes when `LIMIT`,
 `DISTINCT` and aggregation are not used.

 I think that ordering wouldn't be disabled if your function was properly
 marked as being an aggregate (as it's one)

 {{{#!python
 children_query = MyModel.objects.filter(
     parent=OuterRef('pk'),
 ).annotate(
     child_names=Aggregate('name', function='group_concat'),
 ).values('child_names').order_by('name')
 }}}

 If you use `Func` instead of `Aggregate` then the ORM has to way to know
 your opaque expression is performing aggregation and that ordering might
 be relevant.

 If using `Aggregate` instead of `Func` still results in cleared ordering
 then please re-open this ticket as it shouldn't be the case otherwise this
 a clearly a misuse of the ORM and likely only work in your case because
 MySQL is very lax about `GROUP BY` requirements.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32658#comment:2>
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/075.e92c1938edbc44257a851335c2c1ffdf%40djangoproject.com.

Reply via email to