#24462: Add a new QuerySet operation to use current results as a subquery
-------------------------------------+-------------------------------------
     Reporter:  trosos               |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  distinct order_by    |             Triage Stage:  Accepted
  subquery                           |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jameel Al-Aziz):

 I believe we have a similar need, but for difference reasons.

 We have a JSONB postgres column and want to `jsonb_array_elements`
 followed by filtering. Because `jsonb_array_elements` expands into a set
 of columns, you cannot apply a `where` clause filter on the function
 column. A simple solution is to use `jsonb_array_elements` in a subquery
 and then filter the resulting subquery.

 Another use case for sub-querying is being able to apply filters on union
 queries. Our actual use case involves both `jsonb_array_elements` and
 `UNION`s.

 A sample query that I'm trying to write with querysets (but so far have
 been unable to) is:
 {{{
 select *
 from (
         select id, jsonb_array_elements(json_data->'some_array') as elem
         from foo as foo1
         union
         select id, jsonb_array_elements(json_data->'other_array') as elem
         from foo as foo2
 ) as foo_w_elems
 where (elem->>'subfield')::int in (
         select id
         from bar
         where expires_at >= CURRENT_TIMESTAMP
 )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/24462#comment:9>
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/064.abc772573db3c1677775544bd5b50299%40djangoproject.com.

Reply via email to