Opened 7 years ago

Closed 7 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 7 years ago.

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by miracle2k

comment:1 Changed 7 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 7 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 7 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.

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