Changes between Version 1 and Version 2 of Ticket #35586, comment 3
- Timestamp:
- Jul 9, 2024, 4:32:12 PM (5 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #35586, comment 3
v1 v2 7 7 {{{#!diff 8 8 diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py 9 index 4ee22420d9.. 589fd30b0f1006449 index 4ee22420d9..8656979630 100644 10 10 --- a/django/db/models/expressions.py 11 11 +++ b/django/db/models/expressions.py … … 20 20 if output_field is not None: 21 21 diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py 22 index 438bb5ddbd.. 0eb1a2476c10064422 index 438bb5ddbd..4f1210c49e 100644 23 23 --- a/django/db/models/sql/query.py 24 24 +++ b/django/db/models/sql/query.py 25 @@ -491,6 +491,1 0@@ def get_aggregation(self, using, aggregate_exprs):25 @@ -491,6 +491,11 @@ def get_aggregation(self, using, aggregate_exprs): 26 26 ) 27 27 or having 28 28 ) 29 + has_set_returning_annotation = any( 30 + getattr(annotation, "set_returning", False) 31 + for annotation in self.annotations.values() 32 + ) 29 + set_returning_annotations = { 30 + alias 31 + for alias, annotation in self.annotation_select.items() 32 + if getattr(annotation, "set_returning", False) 33 + } 33 34 # Decide if we need to use a subquery. 34 35 # 35 36 # Existing aggregations would cause incorrect results as 36 @@ -510,6 +51 4,7 @@ def get_aggregation(self, using, aggregate_exprs):37 @@ -510,6 +515,7 @@ def get_aggregation(self, using, aggregate_exprs): 37 38 or qualify 38 39 or self.distinct 39 40 or self.combinator 40 + or has_set_returning_annotation41 + or set_returning_annotations 41 42 ): 42 43 from django.db.models.sql.subqueries import AggregateQuery 44 45 @@ -550,6 +556,9 @@ def get_aggregation(self, using, aggregate_exprs): 46 for annotation_alias, annotation in self.annotation_select.items(): 47 if annotation.get_group_by_cols(): 48 annotation_mask.add(annotation_alias) 49 + # Annotations that possibly return multiple rows cannot 50 + # be masked as they might have an incidence on the query. 51 + annotation_mask |= set_returning_annotations 52 inner_query.set_annotation_mask(annotation_mask) 53 54 # Add aggregates to the outer AggregateQuery. This requires making 43 55 }}} 44 56