#36096: QuerySet.values() on a UNION query results in a broken query
-------------------------------------+-------------------------------------
     Reporter:  Isaac Nygaard        |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.0                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 When calling `QuerySet.values()` on a union query, I get some internal
 Django errors. For example:

 {{{
 qs = model.values(*grouping).annotate(**annotations)

 // SAMPLE A: the following raises internal Django errors
 t1 = qs.annotate(time_period=Value(1)).filter(filter_1)
 t2 = qs.annotate(time_period=Value(2)).filter(filter_2)
 qs = t1.union(t2).values(*values)

 // SAMPLE B: the following works
 t1 = qs.annotate(time_period=Value(1)).filter(filter_1).values(*values)
 t2 = qs.annotate(time_period=Value(2)).filter(filter_2).values(*values)
 qs = t1.union(t2)
 }}}

 Sample A fails because `qs.annotation_select` becomes different from
 `t1.annotation_select` and `t2.annotation_select`. Django doesn't complain
 until the query is run, and the database returns different columns (or
 columns in a different order) than what it was expecting.

 I would think Django should do one of the following:
 1. have `qs.values()` update the selected annotations for its two child
 combinator queries, t1/t2 (this is what I expected and why I wrote the
 code that way)
 2. disallow `values()` on union queries, e.g. by raising an informative
 error
 3. detect that `qs.annotation_select` columns do not match
 `annotation_select` of the two child combinator queries and raise an
 informative error
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36096>
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 visit 
https://groups.google.com/d/msgid/django-updates/0107019462e56883-a3a44e69-e68d-4925-9045-7d021bfe21b5-000000%40eu-central-1.amazonses.com.

Reply via email to