浏览代码

Fixed #33262 -- Fixed crash of conditional aggregation on Exists().

Hannes Ljungberg 3 年之前
父节点
当前提交
a934d377af

+ 1 - 1
django/contrib/postgres/aggregates/mixins.py

@@ -30,7 +30,7 @@ class OrderableAggMixin:
             sql, sql_params = super().as_sql(compiler, connection, ordering=(
                 'ORDER BY ' + ', '.join(ordering_expr_sql)
             ))
-            return sql, sql_params + ordering_params
+            return sql, (*sql_params, *ordering_params)
         return super().as_sql(compiler, connection, ordering='')
 
     def set_source_expressions(self, exprs):

+ 1 - 1
django/db/models/aggregates.py

@@ -87,7 +87,7 @@ class Aggregate(Func):
                     compiler, connection, template=template, filter=filter_sql,
                     **extra_context
                 )
-                return sql, params + filter_params
+                return sql, (*params, *filter_params)
             else:
                 copy = self.copy()
                 copy.filter = None

+ 8 - 0
tests/aggregation/test_filter_argument.py

@@ -141,3 +141,11 @@ class FilteredAggregateTests(TestCase):
             )
         )
         self.assertEqual(aggregate, {'max_rating': 4.5})
+
+    def test_filtered_aggregate_on_exists(self):
+        aggregate = Book.objects.values('publisher').aggregate(
+            max_rating=Max('rating', filter=Exists(
+                Book.authors.through.objects.filter(book=OuterRef('pk')),
+            )),
+        )
+        self.assertEqual(aggregate, {'max_rating': 4.5})