Undefined order for some (many?) lists
I guess I just assumed that database results are sorted by primary key if nothing else is requested. Turns out I'm wrong.
In practice the order in fact seems to be by pk in most cases, but there are exceptions (e.g. when you change StudySession.schedule_id
with postgres).
There different approaches to deal with this situation:
- Leave as is. Nobody complained so far, so it doesn't seem to be a major issue.
- Fix on a case-by-case basis when it comes up.
- Go through all code and fix all instances of this issue (a lot of work).
- Define default ordering on all models (even though ordering is not necessary in many situations).
Given the low impact and the amount of work (3) would be I slightly lean towards (2).
A special case of this is queries in templates, e.g. {% for session in study.studysession_set.all %}
. The template syntax does not allow to add an order_by('pk')
without further steps. There are again different options, but the simplest one is to move this to get_context_data()
.