diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-06-08 07:21:54 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-06-08 07:23:09 +0200 |
| commit | df9b9de6b05b2dca226e306791e4f06aab7d85e0 (patch) | |
| tree | 30dd9f4e6dd1b2ceefc45b7b386647d53446c790 /tests/annotations/tests.py | |
| parent | 21adaffb6ebe7513c355f49a1628d11df5825800 (diff) | |
[3.1.x] Fixed #31660 -- Fixed queryset crash when grouping by m2o relation.
Regression in 3a941230c85b2702a5e1cd97e17251ce21057efa.
Thanks Tomasz SzymaĆski for the report.
Backport of 78ad4b4b0201003792bfdbf1a7781cbc9ee03539 from master
Diffstat (limited to 'tests/annotations/tests.py')
| -rw-r--r-- | tests/annotations/tests.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py index 142c23ead6..c1ac0516ac 100644 --- a/tests/annotations/tests.py +++ b/tests/annotations/tests.py @@ -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}, + ]) |
