Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#12819 closed (fixed)

Problem with commit 12307 and null onetoone fields

Reported by: s.angel@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: select_related onetoone null
Cc: s.angel@…, George Vilches, Carl Meyer Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The commit [12307] has a bug. If i use select_related with a OneToOneField? which can be null, if for the FIRST object, this relation is null, then it's give me an error.

A path is attached

Attachments (2)

12307-onetoone-null.patch (639 bytes) - added by s.angel@… 10 years ago.
django-select-related.diff (2.4 KB) - added by Alex Gaynor 10 years ago.

Download all attachments as: .zip

Change History (12)

Changed 10 years ago by s.angel@…

Attachment: 12307-onetoone-null.patch added

comment:1 Changed 10 years ago by Karen Tracey

Needs tests: set

I'm having difficulty parsing the prose description. A failing test would be extremely helpful.

comment:2 Changed 10 years ago by s.angel@…

Ok here is an example

class Image(models.Model):
        path = models.CharField(max_length=50)

class Product(models.Model):
        name  = models.CharField(max_length=50)
        image = models.OneToOneField('Image', blank=True, null=True)
from mytestapp.model import Image, Product

image1 = Image(path='image1')

product1 = Product(name='product1', image=image1)

# add a product without image
product2 = Product(name='product2')

print '%d products' % len(Product.objects.all().select_related('image'))

=> AttributeError: 'NoneType' object has no attribute '_product_cache'

# now set an image for the second product
image2 = Image(path='image2')

product2.image = image2

print '%d products' % len(Product.objects.all().select_related('image'))

=> 2 products

Traceback for the AttribueError:

/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in __len__(self)
     77                 self._result_cache = list(self._iter)
     78             else:
---> 79                 self._result_cache = list(self.iterator())
     80         elif self._iter:
     81             self._result_cache.extend(list(self._iter))

/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in iterator(self)
    270                             index_start, max_depth,
    271                             requested=requested, offset=len(aggregate_select),
--> 272                             only_load=only_load)
    273             else:
    274                 if skip:

/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in get_cached_row(klass, row, index_start, max_depth, cur_depth, requested, offset, only_load)
   1207                 # If the field is unique, populate the

   1208                 # reverse descriptor cache on the related object

-> 1209                 setattr(rel_obj, f.related.get_cache_name(), obj)
   1211     # Now do the same, but for reverse related objects.

AttributeError: 'NoneType' object has no attribute '_product_cache'

No problem if a gave an image to product2. In my previous message i said "the first" because nn my complex app with many select_related, in some case the error doesn't occur, when the first row retrieved has a non-null value for the related field. But i can't do it with this short example, the error occurs all the time (if at less one image is null)

comment:3 Changed 10 years ago by George Vilches

Cc: George Vilches added

comment:4 Changed 10 years ago by Carl Meyer

Cc: Carl Meyer added

comment:5 Changed 10 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

Changed 10 years ago by Alex Gaynor

Attachment: django-select-related.diff added

comment:6 Changed 10 years ago by Alex Gaynor

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:7 Changed 10 years ago by jkocherhans

Resolution: fixed
Status: newclosed

(In [12543]) Fixed #12819. Fixed a bug with caching values of nullable 1to1 fields. Thanks, s.angel@… for the initial patch, and Alex Gaynor for the tests.

comment:8 Changed 10 years ago by Yeago

see also #12937

comment:9 in reply to:  8 Changed 10 years ago by Karen Tracey

Replying to subsume:

see also #12937

Please explain? You've added a comment to see another ticket (which already had a mention back to this ticket). Near as I can tell that ticket notes that the problem reported there includes an instance of the problem reported here, but that they are different problems. This one is supposed to be fixed by r12543, that one is still open. What are you trying to accomplish by noting 'see #12937'?

comment:10 Changed 8 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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