#31217: QuerySet.values()/values_list() with ordering by annotations with 
related
fields and aggregations crashes.
-------------------------------------+-------------------------------------
     Reporter:  felixxm              |                    Owner:  felixxm
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  3.0
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by felixxm:

Old description:

> `QuerySet.values()`/`values_list()` with ordering by annotations with
> related fields and aggregations crashes, e.g.
> {{{
>     def test_aggregation_ordered_by_related_annotation_values(self):
>         from django.db.models.functions import Coalesce
>         self.assertEqual(
>             list(Book.objects.annotate(
>                 min_age=Min('authors__age'),
>             ).annotate(
>                 min_related_age=Coalesce('min_age', 'contact__age'),
>             ).order_by('min_related_age').values_list('pk', flat=True)),
>             [self.b4.pk, self.b3.pk, self.b1.pk, self.b2.pk, self.b5.pk,
> self.b6.pk],
>         )
> }}}
> crashes with:
> {{{
> django.db.utils.ProgrammingError: column "t4.age" must appear in the
> GROUP BY clause or be used in an aggregate function
> LINE 1: ...ER BY COALESCE(COUNT("aggregation_author"."age"), T4."age")
> ...
> }}}
>
> Regression in 59b4e99dd00b9c36d56055b889f96885995e4240.
>
> Thanks Jon Dufresne for the report.

New description:

 `QuerySet.values()`/`values_list()` with ordering by annotations with
 related fields and aggregations crashes, e.g.
 {{{
     def test_aggregation_ordered_by_related_annotation_values(self):
         from django.db.models.functions import Coalesce
         self.assertEqual(
             list(Book.objects.annotate(
                 min_age=Min('authors__age'),
             ).annotate(
                 min_related_age=Coalesce('min_age', 'contact__age'),
             ).order_by('min_related_age').values_list('pk', flat=True)),
             [self.b4.pk, self.b3.pk, self.b1.pk, self.b2.pk, self.b5.pk,
 self.b6.pk],
         )
 }}}
 crashes with:
 {{{
 django.db.utils.ProgrammingError: column "t4.age" must appear in the GROUP
 BY clause or be used in an aggregate function
 LINE 1: ...ER BY COALESCE(COUNT("aggregation_author"."age"), T4."age") ...
 }}}

 Regression in 59b4e99dd00b9c36d56055b889f96885995e4240.

 The query before:
 {{{
 ....
 GROUP BY "aggregation_book"."id", T4."age" ORDER BY
 COALESCE(MIN("aggregation_author"."age"), T4."age") ASC
 }}}
 and after 59b4e99dd00b9c36d56055b889f96885995e4240
 {{{
 ...
 GROUP BY "aggregation_book"."id" ORDER BY
 COALESCE(MIN("aggregation_author"."age"), T4."age") ASC
 }}}

 Thanks Jon Dufresne for the report.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31217#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/065.5c5a91cf7834110806714d31eb9622ce%40djangoproject.com.

Reply via email to