#29694: QuerySet.values_list() combined with .extra() or .annotate() may produce
wrong .union()
-------------------------------------+-------------------------------------
     Reporter:  master               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:  union values_list    |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by master):

 (I wasn't sure and I don't mind to reopen a previous ticket, but my
 reasoning to open a new ticket was exactly the one of Tim).

 So why didn't the test case (test_union_with_two_annotated_values_list)
 fail after the changes of #29286? Because it is written with annotate()
 instead of extra(). To reproduce the steps of the demo code of #29229,
 replace:

 {{{
 #!python
 qs1 = Number.objects.filter(num=1).annotate(count=Value(0,
 IntegerField()),).values_list('num', 'count')
 }}}
 by:
 {{{
 #!python
 qs1 = Number.objects.filter(num=1).extra(select={'count':
 0}).values_list('num', 'count')
 }}}

 Note: I just discovered that the usage of extra() is discouraged, and I
 will consider a replacement, but not as a priority.

 My suggestion for a fix in
 django\db\models\sql\compiler.py\get_combinator_sql(), to work both with
 extra and annotate, is something of this kind:
 {{{
 #!python
 if (not compiler.query.values_select and not compiler.query.annotations
 and (self.query.values_select or self.query.annotation_select or
 self.query.extra_select)):
     compiler.query.set_values(tuple(self.query.values_select) +
             tuple(self.query.annotation_select) +
 tuple(self.query.extra_select)
     )
 }}}

 I'm not sure if we need the additional condition "and not
 compiler.query.<extra>", as I don't know what <extra> would be amongst:
 "extra", "extra_select" and "_extra".

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29694#comment:3>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.e4368f76fad30d1bb6b1cfa10af772cb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to