Opened 9 years ago

Closed 7 years ago

#2465 closed enhancement (fixed)

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

Reported by: Adam Endicott <leftwing17@…> Owned by: mtredinnick
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@…> 9 years ago.

Download all attachments as: .zip

Change History (9)

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

comment:1 Changed 9 years ago by mtredinnick

  • Summary changed from [patch] doc addition - select_related() and null=True to Make 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 9 years ago by adrian

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 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:4 Changed 8 years ago by dcramer

  • Component changed from Documentation to Database wrapper
  • Keywords qs-rf added
  • Resolution fixed deleted
  • Status changed from closed to reopened

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

comment:5 Changed 8 years ago by jacob

  • Owner changed from jacob to malcolm
  • Status changed from reopened to new

comment:6 Changed 8 years ago by jacob

  • Owner changed from malcolm to mtredinnick

comment:7 Changed 8 years ago by mir

  • Cc mir@… added

comment:8 Changed 7 years ago by mtredinnick

  • Keywords qs-rf removed
  • Resolution set to fixed
  • Status changed from new to closed

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