mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #31660 -- Fixed queryset crash when grouping by m2o relation.
Regression in 3a941230c8
.
Thanks Tomasz Szymański for the report.
This commit is contained in:
parent
07506a6114
commit
78ad4b4b02
3 changed files with 30 additions and 4 deletions
|
@ -1,11 +1,13 @@
|
|||
import datetime
|
||||
from decimal import Decimal
|
||||
from unittest import skipIf
|
||||
|
||||
from django.core.exceptions import FieldDoesNotExist, FieldError
|
||||
from django.db import connection
|
||||
from django.db.models import (
|
||||
BooleanField, CharField, Count, DateTimeField, Exists, ExpressionWrapper,
|
||||
F, Func, IntegerField, Max, NullBooleanField, OuterRef, Q, Subquery, Sum,
|
||||
Value,
|
||||
BooleanField, Case, CharField, Count, DateTimeField, Exists,
|
||||
ExpressionWrapper, F, Func, IntegerField, Max, NullBooleanField, OuterRef,
|
||||
Q, Subquery, Sum, Value, When,
|
||||
)
|
||||
from django.db.models.expressions import RawSQL
|
||||
from django.db.models.functions import Length, Lower
|
||||
|
@ -632,3 +634,22 @@ class NonAggregateAnnotationTestCase(TestCase):
|
|||
datetime.date(2008, 6, 23),
|
||||
datetime.date(2008, 11, 3),
|
||||
])
|
||||
|
||||
@skipIf(
|
||||
connection.vendor == 'mysql' and 'ONLY_FULL_GROUP_BY' in connection.sql_mode,
|
||||
'GROUP BY optimization does not work properly when ONLY_FULL_GROUP_BY '
|
||||
'mode is enabled on MySQL, see #31331.',
|
||||
)
|
||||
def test_annotation_aggregate_with_m2o(self):
|
||||
qs = Author.objects.filter(age__lt=30).annotate(
|
||||
max_pages=Case(
|
||||
When(book_contact_set__isnull=True, then=Value(0)),
|
||||
default=Max(F('book__pages')),
|
||||
output_field=IntegerField(),
|
||||
),
|
||||
).values('name', 'max_pages')
|
||||
self.assertCountEqual(qs, [
|
||||
{'name': 'James Bennett', 'max_pages': 300},
|
||||
{'name': 'Paul Bissex', 'max_pages': 0},
|
||||
{'name': 'Wesley J. Chun', 'max_pages': 0},
|
||||
])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue