Skip to content

Commit

Permalink
[3.0.x] Fixed #31136 -- Disabled grouping by aliases on QuerySet.valu…
Browse files Browse the repository at this point in the history
…es()/values_list().

Regression in fb3f034.

Thanks Sigurd Ljødal for the report.
Backport of 0f843fd from master
  • Loading branch information
felixxm committed Jan 4, 2020
1 parent 02cda09 commit 4f81f6d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,7 +2100,9 @@ def set_values(self, fields):

if self.group_by is True:
self.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
self.set_group_by()
# Disable GROUP BY aliases to avoid orphaning references to the
# SELECT clause which is about to be cleared.
self.set_group_by(allow_aliases=False)
self.clear_select_fields()

if fields:
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/3.0.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ Bugfixes
* Fixed a regression in Django 3.0 that caused a crash when subtracting
``DateField``, ``DateTimeField``, or ``TimeField`` from a ``Subquery()``
annotation (:ticket:`31133`).

* Fixed a regression in Django 3.0 where ``QuerySet.values()`` and
``values_list()`` crashed if a queryset contained an aggregation and
``Exists()`` annotation (:ticket:`31136`).
14 changes: 14 additions & 0 deletions tests/aggregation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,20 @@ def test_aggregation_subquery_annotation_exists(self):
)
self.assertTrue(publisher_qs.exists())

def test_aggregation_exists_annotation(self):
published_books = Book.objects.filter(publisher=OuterRef('pk'))
publisher_qs = Publisher.objects.annotate(
published_book=Exists(published_books),
count=Count('book'),
).values_list('name', flat=True)
self.assertCountEqual(list(publisher_qs), [
'Apress',
'Morgan Kaufmann',
"Jonno's House of Books",
'Prentice Hall',
'Sams',
])

@skipUnlessDBFeature('supports_subqueries_in_group_by')
def test_group_by_subquery_annotation(self):
"""
Expand Down

0 comments on commit 4f81f6d

Please sign in to comment.