including a nullable foreign key reference in Meta ordering has unexpected results
|Reported by:||Owned by:||Malcolm Tredinnick|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
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.
Change History (14)
comment:1 Changed 9 years ago by
|Summary:||including a nullable field in Meta ordering has unexpected results → including a nullable foreign key reference in Meta ordering has unexpected results|
comment:10 Changed 8 years ago by
|Owner:||changed from George Vilches to Malcolm Tredinnick|
|Status:||new → assigned|