Changes between Version 1 and Version 2 of Ticket #35586, comment 3


Ignore:
Timestamp:
Jul 9, 2024, 4:32:12 PM (5 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35586, comment 3

    v1 v2  
    77{{{#!diff
    88diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
    9 index 4ee22420d9..589fd30b0f 100644
     9index 4ee22420d9..8656979630 100644
    1010--- a/django/db/models/expressions.py
    1111+++ b/django/db/models/expressions.py
     
    2020         if output_field is not None:
    2121diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    22 index 438bb5ddbd..0eb1a2476c 100644
     22index 438bb5ddbd..4f1210c49e 100644
    2323--- a/django/db/models/sql/query.py
    2424+++ b/django/db/models/sql/query.py
    25 @@ -491,6 +491,10 @@ def get_aggregation(self, using, aggregate_exprs):
     25@@ -491,6 +491,11 @@ def get_aggregation(self, using, aggregate_exprs):
    2626             )
    2727             or having
    2828         )
    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+        }
    3334         # Decide if we need to use a subquery.
    3435         #
    3536         # Existing aggregations would cause incorrect results as
    36 @@ -510,6 +514,7 @@ def get_aggregation(self, using, aggregate_exprs):
     37@@ -510,6 +515,7 @@ def get_aggregation(self, using, aggregate_exprs):
    3738             or qualify
    3839             or self.distinct
    3940             or self.combinator
    40 +            or has_set_returning_annotation
     41+            or set_returning_annotations
    4142         ):
    4243             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
    4355}}}
    4456
Back to Top