Fixed #32200 -- Fixed grouping by ExpressionWrapper() with Q objects.

Thanks Gordon Wrigley for the report.

Regression in df32fd42b8.
This commit is contained in:
Hasan Ramezani 2020-11-17 23:31:58 +01:00 committed by Mariusz Felisiak
parent ead37dfb58
commit fe9c7ded29
3 changed files with 22 additions and 3 deletions

View File

@ -915,9 +915,13 @@ class ExpressionWrapper(Expression):
return [self.expression] return [self.expression]
def get_group_by_cols(self, alias=None): def get_group_by_cols(self, alias=None):
expression = self.expression.copy() if isinstance(self.expression, Expression):
expression.output_field = self.output_field expression = self.expression.copy()
return expression.get_group_by_cols(alias=alias) expression.output_field = self.output_field
return expression.get_group_by_cols(alias=alias)
# For non-expressions e.g. an SQL WHERE clause, the entire
# `expression` must be included in the GROUP BY clause.
return super().get_group_by_cols()
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
return compiler.compile(self.expression) return compiler.compile(self.expression)

View File

@ -21,3 +21,6 @@ Bugfixes
* Fixed a regression in Django 3.1 that caused a crash of auto-reloader for * Fixed a regression in Django 3.1 that caused a crash of auto-reloader for
certain invocations of ``runserver`` on Windows with Python 3.7 and below certain invocations of ``runserver`` on Windows with Python 3.7 and below
(:ticket:`32202`). (:ticket:`32202`).
* Fixed a regression in Django 3.1 that caused the incorrect grouping by a
``Q`` object annotation (:ticket:`32200`).

View File

@ -195,6 +195,18 @@ class NonAggregateAnnotationTestCase(TestCase):
self.assertEqual(book.isnull_pubdate, False) self.assertEqual(book.isnull_pubdate, False)
self.assertEqual(book.rating_count, 1) self.assertEqual(book.rating_count, 1)
@skipUnlessDBFeature('supports_boolean_expr_in_select_clause')
def test_grouping_by_q_expression_annotation(self):
authors = Author.objects.annotate(
under_40=ExpressionWrapper(Q(age__lt=40), output_field=BooleanField()),
).values('under_40').annotate(
count_id=Count('id'),
).values('under_40', 'count_id')
self.assertCountEqual(authors, [
{'under_40': False, 'count_id': 3},
{'under_40': True, 'count_id': 6},
])
def test_aggregate_over_annotation(self): def test_aggregate_over_annotation(self):
agg = Author.objects.annotate(other_age=F('age')).aggregate(otherage_sum=Sum('other_age')) agg = Author.objects.annotate(other_age=F('age')).aggregate(otherage_sum=Sum('other_age'))
other_agg = Author.objects.aggregate(age_sum=Sum('age')) other_agg = Author.objects.aggregate(age_sum=Sum('age'))