From c9b9a52edc66be117c6e5b5214fa788a4d5db7a8 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 1 Aug 2023 16:16:28 +0200 Subject: Fixed #34750 -- Fixed QuerySet.count() when grouping by unused multi-valued annotations. Thanks Toan Vuong for the report. Thanks Simon Charette for the review. Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7. --- django/db/models/sql/query.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'django/db/models/sql/query.py') diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ac735012f1..220ae92754 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -498,6 +498,11 @@ class Query(BaseExpression): annotation_mask |= expr.get_refs() for aggregate in aggregates.values(): annotation_mask |= aggregate.get_refs() + # Avoid eliding expressions that might have an incidence on + # the implicit grouping logic. + for annotation_alias, annotation in self.annotation_select.items(): + if annotation.get_group_by_cols(): + annotation_mask.add(annotation_alias) inner_query.set_annotation_mask(annotation_mask) # Add aggregates to the outer AggregateQuery. This requires making -- cgit v1.3