Opened 16 years ago

Closed 16 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: dev
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

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

Download all attachments as: .zip

Change History (4)

by miracle2k, 16 years ago

Attachment: 7716-select-related.diff added

comment:1 by miracle2k, 16 years ago

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

comment:2 by Malcolm Tredinnick, 16 years ago

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

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