Opened 7 years ago

Closed 3 years ago

#10657 closed Bug (fixed)

Meta option 'ordering' causes problems with nullable fields

Reported by: andrewl Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Normal Keywords: ordering, order_by
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


See also ticket:10574 which basically seems to be talking about the same thing under a slightly different setup.

Specifying fields which may be null in the Meta option 'ordering' causes problems later on when retrieving model records.

See the attached Model definition and fixture for an example. Note that the Model definition appears to require one 'level' of inheritance for the problem to occur (hence the Animal-Person/Pet relationship) - the model and fixture files with the suffix 'safe' demonstrate a similar setup which does not suffer from the problem.

To reproduce the problem, set up the example application with the fixture, and try House.objects.all().count(), which will return the correct count of 2. However House.objects.all() will return only 1 House - the missing House has a null record for the 'pet' field. House.objects.all().order_by() returns both Houses.

The problem should either be fixed or (as I recognise that the underlying problem is a tad complex) the Model Meta options documentation on the 'ordering' option should include a note to the effect that 'Ordering by nullable fields, though legal, is unpredictable'.

Attachments (1) (1.4 KB) - added by andrewl 7 years ago.
Sample Model and Initial Data

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by andrewl

Sample Model and Initial Data

comment:1 Changed 7 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 5 years ago by julien

  • Component changed from Core framework to Database layer (models, ORM)

comment:3 Changed 5 years ago by sethtrain

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 3 years ago by akaariai

  • Resolution set to fixed
  • Status changed from new to closed

This has been fixed a long time ago.

Note: See TracTickets for help on using tickets.
Back to Top