mirror of
https://github.com/django/django.git
synced 2025-06-07 04:29:12 +00:00
Fixed #36405 -- Fixed Aggregate.order_by using OuterRef.
co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
parent
b8e5a8a9a2
commit
c2615a0500
@ -120,11 +120,6 @@ class Aggregate(Func):
|
|||||||
):
|
):
|
||||||
# Aggregates are not allowed in UPDATE queries, so ignore for_save
|
# Aggregates are not allowed in UPDATE queries, so ignore for_save
|
||||||
c = super().resolve_expression(query, allow_joins, reuse, summarize)
|
c = super().resolve_expression(query, allow_joins, reuse, summarize)
|
||||||
c.order_by = (
|
|
||||||
c.order_by.resolve_expression(query, allow_joins, reuse, summarize)
|
|
||||||
if c.order_by
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
if summarize:
|
if summarize:
|
||||||
# Summarized aggregates cannot refer to summarized aggregates.
|
# Summarized aggregates cannot refer to summarized aggregates.
|
||||||
for ref in c.get_refs():
|
for ref in c.get_refs():
|
||||||
|
@ -25,3 +25,7 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed a regression in Django 5.2 that caused a crash when using ``OuterRef``
|
* Fixed a regression in Django 5.2 that caused a crash when using ``OuterRef``
|
||||||
in the ``filter`` argument of an ``Aggregate`` expression (:ticket:`36404`).
|
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`).
|
||||||
|
@ -2413,6 +2413,35 @@ class AggregateTestCase(TestCase):
|
|||||||
}
|
}
|
||||||
self.assertEqual(values, expected_values)
|
self.assertEqual(values, expected_values)
|
||||||
|
|
||||||
|
@skipUnlessDBFeature("supports_aggregate_order_by_clause")
|
||||||
|
def test_string_agg_filter_outerref(self):
|
||||||
|
values = (
|
||||||
|
Publisher.objects.annotate(
|
||||||
|
stringagg=Subquery(
|
||||||
|
Book.objects.annotate(
|
||||||
|
stringagg=StringAgg(
|
||||||
|
"name",
|
||||||
|
delimiter=Value(";"),
|
||||||
|
order_by=OuterRef("num_awards"),
|
||||||
|
)
|
||||||
|
).values("stringagg")[:1]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.values("stringagg")
|
||||||
|
.order_by("id")
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertQuerySetEqual(
|
||||||
|
values,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"stringagg": "The Definitive Guide to Django: "
|
||||||
|
"Web Development Done Right"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
* 5,
|
||||||
|
)
|
||||||
|
|
||||||
@skipUnlessDBFeature("supports_json_field", "supports_aggregate_order_by_clause")
|
@skipUnlessDBFeature("supports_json_field", "supports_aggregate_order_by_clause")
|
||||||
def test_string_agg_jsonfield_order_by(self):
|
def test_string_agg_jsonfield_order_by(self):
|
||||||
Employee.objects.bulk_create(
|
Employee.objects.bulk_create(
|
||||||
|
@ -364,6 +364,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):
|
def test_bit_and_general(self):
|
||||||
values = AggregateTestModel.objects.filter(integer_field__in=[0, 1]).aggregate(
|
values = AggregateTestModel.objects.filter(integer_field__in=[0, 1]).aggregate(
|
||||||
bitand=BitAnd("integer_field")
|
bitand=BitAnd("integer_field")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user