#33655: Unnecessary column in a GROUP BY clause with QuerySet.exists()
-------------------------------------+-------------------------------------
     Reporter:  Marc Perrin          |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  4.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  exists group by      |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Maybe we should stop relying on ''extra'' and use `Value(1)` instead
 
[https://github.com/django/django/blob/ed0a2c3238aa0b9c0d01c436d5bcd70930d696b0/django/db/models/expressions.py#L990-L991
 which already disables grouping].

 {{{#!diff
 diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
 index 64e7927f7a..1097de4f79 100644
 --- a/django/db/models/sql/query.py
 +++ b/django/db/models/sql/query.py
 @@ -27,6 +27,7 @@
      OuterRef,
      Ref,
      ResolvedOuterRef,
 +    Value,
  )
  from django.db.models.fields import Field
  from django.db.models.fields.related_lookups import MultiColSource
 @@ -582,8 +583,7 @@ def exists(self, using, limit=True):
          q.clear_ordering(force=True)
          if limit:
              q.set_limits(high=1)
 -        q.add_extra({"a": 1}, None, None, None, None, None)
 -        q.set_extra_mask(["a"])
 +        q.add_annotation(Value(1), "a")
          return q

      def has_results(self, using):
 }}}

 Anything that relies on ''extra'' is a black box to the compiler as it
 could be an aggregate function or not.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33655#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701805ef3019d-5ed3f947-93e0-4daf-b481-4775e55c78b3-000000%40eu-central-1.amazonses.com.

Reply via email to