#28333: Filter and subquery for window expressions
-------------------------------------+-------------------------------------
     Reporter:  Mads Jensen          |                    Owner:  Simon
                                     |  Charette
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  window orm filter    |             Triage Stage:  Accepted
  subquery GSoC                      |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by master):

 My code was working till 3.2, but with
 
https://code.djangoproject.com/changeset/8c3046daade8d9b019928f96e53629b03060fe73,
 it doesn't anymore.

 Here is a simplified (no filter(), less annotate(), fake Value(), ...)
 demonstrator:
 {{{
 >>> from postman.models import Message
 >>> qs1=Message.objects.values_list('id').order_by()
 >>> print(qs1.query) # correct
 SELECT "postman_message"."id" FROM "postman_message"
 >>> qs2=Message.objects.values('thread').annotate(id=Value(2,
 IntegerField())).values_list('id').order_by()
 >>> print(qs2.query) # correct
 SELECT 2 AS "id" FROM "postman_message"
 >>> print(qs1.union(qs2).query) # will cause my problem
 SELECT "postman_message"."id" AS "col1" FROM "postman_message" UNION
 SELECT 2 AS "id" FROM "postman_message"
 }}}
 The noticeable point is the introduction of the alias `AS "col1"`, not
 compatible with the `id` in the second part.

 In the full code, the union is injected in another query, of the form
 `SELECT ... FROM ... INNER JOIN (the union) PM ON (... = PM.id)`
 So it leads to the error: `django.db.utils.OperationalError: no such
 column: PM.id`

 In db/models/sql/compiler.py, get_combinator_sql(), the call is imposed
 as: `as_sql(with_col_aliases=True)`

 I don't know how to solve this problem.
 Any advice?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28333#comment:35>
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/010701894f7378ea-32324be8-76ab-4ed6-8c64-1954fd9074bc-000000%40eu-central-1.amazonses.com.

Reply via email to