Code

Opened 6 years ago

Closed 6 years ago

#7716 closed (fixed)

select_related() with nullable ForeignKeys screws up when rows actually have a null value

Reported by: miracle2k Owned by: mtredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

class Review(models.Model):
   author = models.ForeignKey(Person, blank=True, null=True)
>>> Review.objects.filter(author__isnull=True)[0].author
None
>>> Review.objects.select_related('author').filter(author__isnull=True)[0].author
<type 'exceptions.TypeError'>: coercing to Unicode: need string or buffer, NoneType found

>>> r = Review.objects.select_related('author').filter(author__isnull=True)[0]
>>> type(r.author)
<class 'proj.app.models.person.Person'>

Attachments (1)

7716-select-related.diff (515 bytes) - added by miracle2k 6 years ago.

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by miracle2k

comment:1 Changed 6 years ago by miracle2k

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

See attached file; this is probably not the best way to fix this, but it makes the problem go away ;)

comment:2 Changed 6 years ago by mtredinnick

  • Owner changed from nobody to mtredinnick
  • Triage Stage changed from Unreviewed to Accepted

Yes, that is very unlikely to the way to fix this. It's at the wrong level. I'm looking at this. I suspect it might be related to #7530 and a couple of other semi-reports I've seen. I'm not going to apply any workaround; will fix it properly.

comment:3 Changed 6 years ago by mtredinnick

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

(In [8098]) Fixed #7530, #7716 -- When using select_related() and encountering a NULL
related object, populate the attribute correctly. Patch from Bastien Kleineidam.

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.