﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
7512	including a nullable foreign key reference in Meta ordering has unexpected results	R. Bailey <bailey@…>	Malcolm Tredinnick	"In a model such as

{{{
model Stuff(models.Model):
  whatever = models.ForeignKey(Whatever, null=True)
...

class Meta:
   ordering = ['whatever__someattribute', ... ]
}}}

selecting via Stuff.objects.all will yield only those rows for which the foreign key reference is not null.

further, selecting via a Stuff.objects.get(pk=someval), where the corresponding row in Stuff has a null in the foreign key reference will yield a no such PK exception.

the cause is the inner join in the resulting query.

I don't know if I'd argue that either all() or get() should actually allow sorting by a nullable field, but I do think that the exception should be distinct in situations where a PK is missing from join results, and not from the table corresponding to the model being selected against.




"		closed	Database layer (models, ORM)	dev		fixed	1.0-blocker		Accepted	1	0	0	1	0	0
