From f88fc72da4eb76f2d464edb4874ef6046f8a8658 Mon Sep 17 00:00:00 2001 From: Alexander Kerkum Date: Fri, 16 Sep 2022 16:47:50 +0200 Subject: [PATCH] Fixed #34016 -- Fixed QuerySet.values()/values_list() crash on ArrayAgg() and JSONBAgg(). Regression in e06dc4571ea9fd5723c8029959b95808be9f8812. --- django/contrib/postgres/aggregates/mixins.py | 7 +++++-- docs/releases/4.1.2.txt | 4 ++++ tests/postgres_tests/test_aggregates.py | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/django/contrib/postgres/aggregates/mixins.py b/django/contrib/postgres/aggregates/mixins.py index b2f4097b8f..340a917879 100644 --- a/django/contrib/postgres/aggregates/mixins.py +++ b/django/contrib/postgres/aggregates/mixins.py @@ -14,10 +14,13 @@ class OrderableAggMixin: return super().resolve_expression(*args, **kwargs) def get_source_expressions(self): - return super().get_source_expressions() + [self.order_by] + if self.order_by.source_expressions: + return super().get_source_expressions() + [self.order_by] + return super().get_source_expressions() def set_source_expressions(self, exprs): - *exprs, self.order_by = exprs + if isinstance(exprs[-1], OrderByList): + *exprs, self.order_by = exprs return super().set_source_expressions(exprs) def as_sql(self, compiler, connection): diff --git a/docs/releases/4.1.2.txt b/docs/releases/4.1.2.txt index 546ab7a635..4c9b0a60a9 100644 --- a/docs/releases/4.1.2.txt +++ b/docs/releases/4.1.2.txt @@ -18,3 +18,7 @@ Bugfixes * Fixed a bug in Django 4.1 that caused an incorrect validation of ``CheckConstraint`` on ``NULL`` values (:ticket:`33996`). + +* Fixed a regression in Django 4.1 that caused a + ``QuerySet.values()/values_list()`` crash on ``ArrayAgg()`` and + ``JSONBAgg()`` (:ticket:`34016`). diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index c2f38e42b6..9cca121802 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -686,6 +686,15 @@ class TestGeneralAggregate(PostgreSQLTestCase): ], ) + def test_values_list(self): + tests = [ArrayAgg("integer_field"), JSONBAgg("integer_field")] + for aggregation in tests: + with self.subTest(aggregation=aggregation): + self.assertCountEqual( + AggregateTestModel.objects.values_list(aggregation), + [([0],), ([1],), ([2],), ([0],)], + ) + class TestAggregateDistinct(PostgreSQLTestCase): @classmethod