#13312: order_by on null-field gives different results on different db engines
---------------------------------------------------+------------------------
Reporter: binary | Owner: nobody
Status: closed | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Resolution: wontfix | Keywords: ordering,
order_by, null
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by chexum):
* keywords: ordering, order_by => ordering, order_by, null
Comment:
Just to document it, there is a very easy way to control this behavior.
The most portable seems to be an additional order by for "field IS NULL".
It puts NULLs last in at least sqlite3, mysql and PostgreSQL. The reverse
is either "field IS NULL DESC" or "field IS NOT NULL".
It's also easy to use in a Django way:
{{{
q = q.extra(select={'null1':'field1 is null','null2':'field2 is
null'})
q = q.extra(order_by=['null1','-field1','-null2','field2'])
}}}
Because it's so easy to do in a standard way, it might be worth at some
point to add a specific queryset for it (not necessarily modifying
order_by()), as extra() is not always desirable. Having a new queryset
would allow backend specific optimization, like nulls last/first if
supported.
--
Ticket URL: <http://code.djangoproject.com/ticket/13312#comment:3>
Django <http://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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.