﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31217	QuerySet.values()/values_list() with ordering by annotations with related fields and aggregations crashes.	Mariusz Felisiak	Mariusz Felisiak	"`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.
"	Bug	assigned	Database layer (models, ORM)	3.0	Release blocker				Accepted	0	0	0	0	0	0
