#12819 closed (fixed)
Problem with commit 12307 and null onetoone fields
Reported by: | 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)
Change History (12)
by , 15 years ago
Attachment: | 12307-onetoone-null.patch added |
---|
comment:1 by , 15 years ago
Needs tests: | set |
---|
comment:2 by , 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 , 15 years ago
Cc: | added |
---|
comment:4 by , 15 years ago
Cc: | added |
---|
comment:5 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 15 years ago
Attachment: | django-select-related.diff added |
---|
comment:6 by , 15 years ago
Needs tests: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:7 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:9 by , 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'?
I'm having difficulty parsing the prose description. A failing test would be extremely helpful.