mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #29416 -- Removed unnecesary subquery from GROUP BY clause on MySQL when using a RawSQL annotation.
Regression in 1d070d027c
.
This commit is contained in:
parent
cd242d185b
commit
4ab1f559e8
@ -157,7 +157,9 @@ class SQLCompiler:
|
|||||||
}
|
}
|
||||||
expressions = [pk] + [
|
expressions = [pk] + [
|
||||||
expr for expr in expressions
|
expr for expr in expressions
|
||||||
if expr in having or getattr(expr, 'alias', None) not in pk_aliases
|
if expr in having or (
|
||||||
|
getattr(expr, 'alias', None) is not None and expr.alias not in pk_aliases
|
||||||
|
)
|
||||||
]
|
]
|
||||||
elif self.connection.features.allows_group_by_selected_pks:
|
elif self.connection.features.allows_group_by_selected_pks:
|
||||||
# Filter out all expressions associated with a table's primary key
|
# Filter out all expressions associated with a table's primary key
|
||||||
|
@ -14,3 +14,6 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed detection of custom URL converters in included patterns
|
* Fixed detection of custom URL converters in included patterns
|
||||||
(:ticket:`29415`).
|
(:ticket:`29415`).
|
||||||
|
|
||||||
|
* Fixed a regression that added an unnecessary subquery to the ``GROUP BY``
|
||||||
|
clause on MySQL when using a ``RawSQL`` annotation (:ticket:`29416`).
|
||||||
|
@ -6,6 +6,7 @@ from django.db.models import (
|
|||||||
BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
|
BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
|
||||||
IntegerField, NullBooleanField, Q, Sum, Value,
|
IntegerField, NullBooleanField, Q, Sum, Value,
|
||||||
)
|
)
|
||||||
|
from django.db.models.expressions import RawSQL
|
||||||
from django.db.models.functions import Length, Lower
|
from django.db.models.functions import Length, Lower
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
|
|
||||||
@ -322,6 +323,17 @@ class NonAggregateAnnotationTestCase(TestCase):
|
|||||||
for publisher in publishers.filter(pk=self.p1.pk):
|
for publisher in publishers.filter(pk=self.p1.pk):
|
||||||
self.assertEqual(publisher['book__rating'], publisher['total'])
|
self.assertEqual(publisher['book__rating'], publisher['total'])
|
||||||
|
|
||||||
|
@skipUnlessDBFeature('allows_group_by_pk')
|
||||||
|
def test_rawsql_group_by_collapse(self):
|
||||||
|
raw = RawSQL('SELECT MIN(id) FROM annotations_book', [])
|
||||||
|
qs = Author.objects.values('id').annotate(
|
||||||
|
min_book_id=raw,
|
||||||
|
count_friends=Count('friends'),
|
||||||
|
).order_by()
|
||||||
|
_, _, group_by = qs.query.get_compiler(using='default').pre_sql_setup()
|
||||||
|
self.assertEqual(len(group_by), 1)
|
||||||
|
self.assertNotEqual(raw, group_by[0])
|
||||||
|
|
||||||
def test_defer_annotation(self):
|
def test_defer_annotation(self):
|
||||||
"""
|
"""
|
||||||
Deferred attributes can be referenced by an annotation,
|
Deferred attributes can be referenced by an annotation,
|
||||||
|
Loading…
Reference in New Issue
Block a user