Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#9425 closed (wontfix)

select_related does not descend ForeignKey fields which can be null

Reported by: fas Owned by: fas
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords: select_related, null, foreignkey, ignored
Cc: simon@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


ForeignKey fields which have the "null" property set to true are ignored by select_related() even though fill_related_selections() is prepared to use a left join for such fields and would deliver correct results.

Attachments (2)

query_utils_patch.diff (404 bytes) - added by fas 10 years ago.
select-related-on-null.patch (4.5 KB) - added by Simon Law 10 years ago.

Download all attachments as: .zip

Change History (8)

Changed 10 years ago by fas

Attachment: query_utils_patch.diff added

comment:1 Changed 10 years ago by fas

Resolution: invalid
Status: newclosed

Changed 10 years ago by Simon Law

comment:2 Changed 10 years ago by Simon Law

Cc: simon@… added
Resolution: invalid
Status: closedreopened

The above patch, select-related-on-null.patch, lets you choose if you want select_related to descend into fields marked as null=True.

Since you can use select_related('field-name') to specify a nullable field, but sometimes you just know that you want all of them.

comment:3 Changed 10 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: reopenedclosed

If you want to descend nullable foreign keys, write them out in the select_related() call. It's not really that onerous. We don't need multiple ways to do this, since selecting across nullable relations is something to be avoided as much as possible.

comment:4 Changed 10 years ago by Simon Law

Resolution: wontfix
Status: closedreopened

It actually is a bit onerous to maintain the list of foreign keys, once you want _one_ nullable foreign key.

That's what follow_nulls is False by default, but exists. For instance, it makes sense to follow null relations when JSON encoding Django models.

comment:5 Changed 10 years ago by Karen Tracey

Resolution: wontfix
Status: reopenedclosed

Please don't reopen tickets closed wontfix by a core developer. The right way to get a wontfix decision reconsidered is to raise the issue on django-developers.

comment:6 Changed 10 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

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