Fixed #33655 -- Removed unnecessary constant from GROUP BY clause for QuerySet.exists().

This commit is contained in:
marcperrinoptel 2022-04-25 15:17:33 -04:00 committed by Mariusz Felisiak
parent dcebc5da48
commit 4282fd468f
2 changed files with 14 additions and 2 deletions

View File

@ -27,6 +27,7 @@ from django.db.models.expressions import (
OuterRef, OuterRef,
Ref, Ref,
ResolvedOuterRef, ResolvedOuterRef,
Value,
) )
from django.db.models.fields import Field from django.db.models.fields import Field
from django.db.models.fields.related_lookups import MultiColSource from django.db.models.fields.related_lookups import MultiColSource
@ -582,8 +583,7 @@ class Query(BaseExpression):
q.clear_ordering(force=True) q.clear_ordering(force=True)
if limit: if limit:
q.set_limits(high=1) q.set_limits(high=1)
q.add_extra({"a": 1}, None, None, None, None, None) q.add_annotation(Value(1), "a")
q.set_extra_mask(["a"])
return q return q
def has_results(self, using): def has_results(self, using):

View File

@ -1434,6 +1434,18 @@ class AggregateTestCase(TestCase):
) )
self.assertTrue(publisher_qs.exists()) self.assertTrue(publisher_qs.exists())
def test_aggregation_filter_exists(self):
publishers_having_more_than_one_book_qs = (
Book.objects.values("publisher")
.annotate(cnt=Count("isbn"))
.filter(cnt__gt=1)
)
query = publishers_having_more_than_one_book_qs.query.exists(
using=connection.alias
)
_, _, group_by = query.get_compiler(connection=connection).pre_sql_setup()
self.assertEqual(len(group_by), 1)
def test_aggregation_exists_annotation(self): def test_aggregation_exists_annotation(self):
published_books = Book.objects.filter(publisher=OuterRef("pk")) published_books = Book.objects.filter(publisher=OuterRef("pk"))
publisher_qs = Publisher.objects.annotate( publisher_qs = Publisher.objects.annotate(