Opened 15 years ago

Closed 15 years ago

Last modified 13 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: dev
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

Description

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@… 15 years ago.
django-select-related.diff (2.4 KB ) - added by Alex Gaynor 15 years ago.

Download all attachments as: .zip

Change History (12)

by s.angel@…, 15 years ago

Attachment: 12307-onetoone-null.patch added

comment:1 by Karen Tracey, 15 years ago

Needs tests: set

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

comment:2 by s.angel@…, 15 years ago

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')
image1.save()

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

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

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')
image2.save()

product2.image = image2
product2.save()

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)
   1210
   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 by George Vilches, 15 years ago

Cc: George Vilches added

comment:4 by Carl Meyer, 15 years ago

Cc: Carl Meyer added

comment:5 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

by Alex Gaynor, 15 years ago

Attachment: django-select-related.diff added

comment:6 by Alex Gaynor, 15 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:7 by jkocherhans, 15 years ago

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 by Yeago, 15 years ago

see also #12937

in reply to:  8 comment:9 by Karen Tracey, 15 years ago

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 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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