Regression in 59b4e99dd00b9c36d56055b889f96885995e4240.
Thanks Jon Dufresne for the report.
--
Ticket URL: <https://code.djangoproject.com/ticket/31217>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => felixxm
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:1>
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(
> max_age=Count('authors__age'),
> ).annotate(
> max_related_age=Coalesce('max_age', 'contact__age'),
> ).order_by('max_related_age').values_list('pk', flat=True)),
> [self.b6.pk, self.b3.pk, self.b2.pk, self.b1.pk, self.b5.pk,
> self.b4.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.
Thanks Jon Dufresne for the report.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:2>
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,
)
}}}
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>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/12388 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:5>
* stage: Ready for checkin => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"6b178a3e930f72069f3cda2e6a09d1b320fc09ec" 6b178a3e]:
{{{
#!CommitTicketReference repository=""
revision="6b178a3e930f72069f3cda2e6a09d1b320fc09ec"
Fixed #31217 -- Made QuerySet.values()/values_list() group by not selected
annotations with aggregations used in order_by().
Regression in 59b4e99dd00b9c36d56055b889f96885995e4240.
Thanks Jon Dufresne for the report and Simon Charette for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:7>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"673444da5e721c0d23809076c5d5d74224457d23" 673444da]:
{{{
#!CommitTicketReference repository=""
revision="673444da5e721c0d23809076c5d5d74224457d23"
[3.0.x] Fixed #31217 -- Made QuerySet.values()/values_list() group by not
selected annotations with aggregations used in order_by().
Regression in 59b4e99dd00b9c36d56055b889f96885995e4240.
Thanks Jon Dufresne for the report and Simon Charette for the review.
Backport of 6b178a3e930f72069f3cda2e6a09d1b320fc09ec from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31217#comment:8>