Opened 10 years ago

Closed 9 years ago

#2465 closed enhancement (fixed)

Make select_related() related work for ForeignKeys with null=True

Reported by: Adam Endicott <leftwing17@…> Owned by: Malcolm Tredinnick
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: mir@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

While trying to figure out why select_related() wasn't doing anything for me, I finally went to the source and realized that it is coded to not work when null=True is set on the relationship. This is something I think should be documented, so here's a patch. Hopefully I'm somewhat close on the markup, I don't know how to test it.

Attachments (1)

db-api.txt.diff (481 bytes) - added by Adam Endicott <leftwing17@…> 10 years ago.

Download all attachments as: .zip

Change History (9)

Changed 10 years ago by Adam Endicott <leftwing17@…>

Attachment: db-api.txt.diff added

comment:1 Changed 10 years ago by Malcolm Tredinnick

Summary: [patch] doc addition - select_related() and null=TrueMake select_related() related work for ForeignKeys with null=True

It would be better to fix select_related() to work properly. There is no real reason for it to be restricted like this. Just to confirm: are you seeing the problem with a ForeignKey field, or only with a ManyToManyField?

comment:2 Changed 10 years ago by Adrian Holovaty

No, I'd rather not change select_related to work for null=True fields. That would involve a bunch of LEFT JOINs in the resulting SQL. select_related does not work on null=True fields on purpose.

comment:3 Changed 10 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

(In [3511]) Fixed #2465 -- Documented the fact that select_related doesn't follow null=True

comment:4 Changed 9 years ago by David Cramer

Component: DocumentationDatabase wrapper
Keywords: qs-rf added
Resolution: fixed
Status: closedreopened

Reopening as this will be fairly easy to support when select_related() allows explicit field calls in queryset refactoring.

comment:5 Changed 9 years ago by Jacob

Owner: changed from Jacob to malcolm
Status: reopenednew

comment:6 Changed 9 years ago by Jacob

Owner: changed from malcolm to Malcolm Tredinnick

comment:7 Changed 9 years ago by Michael Radziej

Cc: mir@… added

comment:8 Changed 9 years ago by Malcolm Tredinnick

Keywords: qs-rf removed
Resolution: fixed
Status: newclosed

Closing, since the original issue has been fixed and the fact that it will work when something like #5020 is dealt with is covered in #5020. The idea is to reduce the number of open tickets, folks, not keep lots of similar ones open.

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