#26634: Add the ability to do subselects for querying versioned data
--------------------------------+--------------------------------------
     Reporter:  dhobbs          |                    Owner:  nobody
         Type:  Uncategorized   |                   Status:  new
    Component:  Uncategorized   |                  Version:  1.8
     Severity:  Normal          |               Resolution:
     Keywords:  QuerySet.extra  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------
Changes (by charettes):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> I have versioned data of the form:
>
> id (autogenerate)
> ref (uuid)
> version (integer)
>
> Each 'save' inserts a new version with the same ref and (version+1)
> version number.
> I'd like an easy way to say 'give me the latest versions' (ie. distinct
> on refs, with max(version)).
>
> The SQL looks like this:
>
> select * from myapp_project where
> id in (SELECT id from myapp_project p WHERE p.version = (SELECT
> max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))
>
> I had to use extra(where=[maxids]) to achieve this*.
>
> *Where maxids = "id in (SELECT id from myapp_project p WHERE p.version =
> (SELECT max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))"
>
> Stackoverflow discussion here:
> http://stackoverflow.com/questions/37303010/what-is-the-django-way-of-
> doing-a-subselect

New description:

 I have versioned data of the form:

 id (autogenerate)
 ref (uuid)
 version (integer)

 Each 'save' inserts a new version with the same ref and (version+1)
 version number.
 I'd like an easy way to say 'give me the latest versions' (ie. distinct on
 refs, with max(version)).

 The SQL looks like this:

 {{{#!sql
 select * from myapp_project where
 id in (SELECT id from myapp_project p WHERE p.version = (SELECT
 max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))
 }}}

 I had to use `extra(where=[maxids])` to achieve this*.

 *Where `maxids = "id in (SELECT id from myapp_project p WHERE p.version =
 (SELECT max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))"`

 Stackoverflow discussion here:
 http://stackoverflow.com/questions/37303010/what-is-the-django-way-of-
 doing-a-subselect

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26634#comment:1>
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.eae96043510fffc8462f1b5ad8d7400b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to