diff --git a/django/contrib/postgres/aggregates/mixins.py b/django/contrib/postgres/aggregates/mixins.py index 6af43a7fea..cd5adacd96 100644 --- a/django/contrib/postgres/aggregates/mixins.py +++ b/django/contrib/postgres/aggregates/mixins.py @@ -27,11 +27,6 @@ class OrderableAggMixin: self.order_by = OrderByList(order_by) super().__init__(*expressions, **extra) - def resolve_expression(self, *args, **kwargs): - if self.order_by is not None: - self.order_by = self.order_by.resolve_expression(*args, **kwargs) - return super().resolve_expression(*args, **kwargs) - def get_source_expressions(self): return super().get_source_expressions() + [self.order_by] diff --git a/docs/releases/5.2.2.txt b/docs/releases/5.2.2.txt index b8deb1c928..a506430b33 100644 --- a/docs/releases/5.2.2.txt +++ b/docs/releases/5.2.2.txt @@ -25,3 +25,7 @@ Bugfixes * Fixed a regression in Django 5.2 that caused a crash when using ``OuterRef`` in the ``filter`` argument of an ``Aggregate`` expression (:ticket:`36404`). + +* Fixed a regression in Django 5.2 that caused a crash when using ``OuterRef`` + in PostgreSQL aggregate functions ``ArrayAgg``, ``StringAgg``, and + ``JSONBAgg`` (:ticket:`36405`). diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index ddd3b6bbd0..0037fb9f83 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -350,6 +350,18 @@ class TestGeneralAggregate(PostgreSQLTestCase): [[], [], [], []], ) + def test_array_agg_with_order_by_outer_ref(self): + StatTestModel.objects.annotate( + atm_ids=Subquery( + AggregateTestModel.objects.annotate( + ids=ArrayAgg( + "id", + order_by=[OuterRef("int1")], + ) + ).values("ids")[:1] + ) + ) + def test_bit_and_general(self): values = AggregateTestModel.objects.filter(integer_field__in=[0, 1]).aggregate( bitand=BitAnd("integer_field")