Re: [Django] #13312: order_by on null-field gives different results on different db engines

7 views
Skip to first unread message

Django

unread,
Feb 18, 2016, 7:18:01 PM2/18/16
to django-...@googlegroups.com
#13312: order_by on null-field gives different results on different db engines
-------------------------------------+-------------------------------------
Reporter: binary | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: ordering, order_by, | Triage Stage:
null | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blueyed):

* ui_ux: => 0
* type: => Uncategorized
* severity: => Normal
* easy: => 0


Comment:

See http://stackoverflow.com/a/35494930/15690, which uses a specilaized
QuerySet/Query.

The ``extra()`` approach is deprecated, and won't work with foreign keys /
joined tables.

--
Ticket URL: <https://code.djangoproject.com/ticket/13312#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 18, 2016, 9:48:21 PM2/18/16
to django-...@googlegroups.com
#13312: order_by on null-field gives different results on different db engines
-------------------------------------+-------------------------------------
Reporter: binary | Owner: nobody
Type: Uncategorized | Status: new

Component: Database layer | Version: 1.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, order_by, | Triage Stage:
null | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* status: closed => new
* resolution: wontfix =>


Comment:

Now that we have expressions, I think it's fine to reopen this ticket. We
can introduce a `OrderNulls.last()/.first()` expression to handle this in
a standard way. We could also just build it right into the `OrderBy`
expression:

{{{
qs.order_by(F('my_field').asc().nullslast())
}}}

If we do build this into the existing order_by, you'll need to be careful
about preserving a 'feature' that stops django from inserting duplicate
ordering clauses (it builds each component then does a string compare to
ensure the new clause isn't a duplicate).

This won't normalise order by nulls across backends, but it'll allow users
to control null handling semantics if they have a use for it. NULL
handling was originally going to be added to the OrderBy expression, but I
ran out of time before that could happen.

--
Ticket URL: <https://code.djangoproject.com/ticket/13312#comment:6>

Reply all
Reply to author
Forward
0 new messages