Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 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@…, gav, carljm Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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

Download all attachments as: .zip

Change History (12)

Changed 4 years ago by s.angel@…

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

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

comment:2 Changed 4 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')
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 Changed 4 years ago by gav

  • Cc gav added

comment:4 Changed 4 years ago by carljm

  • Cc carljm added

comment:5 Changed 4 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by Alex

comment:6 Changed 4 years ago by Alex

  • Needs tests unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:7 Changed 4 years ago by jkocherhans

  • Resolution set to fixed
  • Status changed from new to closed

(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 follow-up: Changed 4 years ago by subsume

see also #12937

comment:9 in reply to: ↑ 8 Changed 4 years ago by kmtracey

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 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.