Opened 10 years ago

Closed 10 years ago

#7716 closed (fixed)

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

Reported by: miracle2k Owned by: Malcolm Tredinnick
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: no UI/UX: no


class Review(models.Model):
   author = models.ForeignKey(Person, blank=True, null=True)
>>> Review.objects.filter(author__isnull=True)[0].author
>>> 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(
<class ''>

Attachments (1)

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

Download all attachments as: .zip

Change History (4)

Changed 10 years ago by miracle2k

Attachment: 7716-select-related.diff added

comment:1 Changed 10 years ago by miracle2k

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

comment:2 Changed 10 years ago by Malcolm Tredinnick

Owner: changed from nobody to Malcolm Tredinnick
Triage Stage: UnreviewedAccepted

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 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(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