Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 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: UI/UX:

Description

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 5 years ago.
select-related-on-null.patch (4.5 KB) - added by sfllaw 5 years ago.

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by fas

comment:1 Changed 5 years ago by fas

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Changed 5 years ago by sfllaw

comment:2 Changed 5 years ago by sfllaw

  • Cc simon@… added
  • Resolution invalid deleted
  • Status changed from closed to reopened

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 5 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 5 years ago by sfllaw

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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 5 years ago by kmtracey

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.