#24218: Use sub-query in ORM when distinct and order_by columns do not match
-------------------------------------+-------------------------------------
     Reporter:  miki725              |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  subquery distinct    |             Triage Stage:  Accepted
  order_by                           |
    Has patch:  1                    |      Needs documentation:  1
  Needs tests:  1                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by carljm):

 Replying to [comment:3 ramiro]:
 > Per the Postgres documentation snipped, shouldn't the `ORDER BY`
 actually be applied to the sub-query to ensure a predictable result?

 I think the idea is that in this case the user has supplied an `ORDER BY`
 which _can't_ be applied to the sub-query (because it doesn't match the
 `DISTINCT ON`), and so we accept unpredictable results in order to provide
 some results which match what the user requested.

 This business of unpredictable results is what makes me most uncomfortable
 with the patch.

 Would it be better to auto-generate an `ORDER BY` for the subquery
 matching the `DISTINCT ON`?

 If we did that, the subquery isn't even needed: we could get the same
 results by just automatically prepending the `ORDER BY` such that it
 matches the `DISTINCT ON`.

 Not sure if that's a good idea, but it seems like perhaps at least as good
 an idea as the one proposed here.

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

Reply via email to