including a nullable foreign key reference in Meta ordering has unexpected results
|Reported by:||R. Bailey <bailey@…>||Owned by:||mtredinnick|
|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 7 years ago by R. Bailey <bailey@…>
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Summary changed from including a nullable field in Meta ordering has unexpected results to including a nullable foreign key reference in Meta ordering has unexpected results
comment:3 Changed 7 years ago by gav
- Component changed from Core framework to Database wrapper
- Has patch set
Changed 7 years ago by gav
comment:5 Changed 7 years ago by ericholscher
- milestone set to 1.0
- Triage Stage changed from Unreviewed to Accepted
comment:10 Changed 7 years ago by mtredinnick
- Owner changed from gav to mtredinnick
- Status changed from new to assigned
comment:12 Changed 7 years ago by mtredinnick
- Resolution set to fixed
- Status changed from assigned to closed