mirror of
https://github.com/django/django.git
synced 2025-01-23 08:39:17 +00:00
Fixed #30120 -- Fixed invalid SQL in distinct aggregate.
Regression in bc05547cd8c1dd511c6b6a6c873a1bc63417b111 (refs #28658).
This commit is contained in:
parent
838e432e3e
commit
65858119d2
@ -68,7 +68,7 @@ class Aggregate(Func):
|
||||
return []
|
||||
|
||||
def as_sql(self, compiler, connection, **extra_context):
|
||||
extra_context['distinct'] = 'DISTINCT' if self.distinct else ''
|
||||
extra_context['distinct'] = 'DISTINCT ' if self.distinct else ''
|
||||
if self.filter:
|
||||
if connection.features.supports_aggregate_filter_clause:
|
||||
filter_sql, filter_params = self.filter.as_sql(compiler, connection)
|
||||
|
@ -8,6 +8,7 @@ from django.db.models import (
|
||||
Avg, Count, DecimalField, DurationField, F, FloatField, Func, IntegerField,
|
||||
Max, Min, Sum, Value,
|
||||
)
|
||||
from django.db.models.expressions import Case, When
|
||||
from django.test import TestCase
|
||||
from django.test.utils import Approximate, CaptureQueriesContext
|
||||
from django.utils import timezone
|
||||
@ -395,6 +396,12 @@ class AggregateTestCase(TestCase):
|
||||
sql = ctx.captured_queries[0]['sql']
|
||||
self.assertIn('SELECT COUNT(*) ', sql)
|
||||
|
||||
def test_count_distinct_expression(self):
|
||||
aggs = Book.objects.aggregate(
|
||||
distinct_ratings=Count(Case(When(pages__gt=300, then='rating')), distinct=True),
|
||||
)
|
||||
self.assertEqual(aggs['distinct_ratings'], 4)
|
||||
|
||||
def test_non_grouped_annotation_not_in_group_by(self):
|
||||
"""
|
||||
An annotation not included in values() before an aggregate should be
|
||||
|
Loading…
x
Reference in New Issue
Block a user