Opened 6 years ago

Closed 5 years ago

#29810 closed Bug (fixed)

Left outer join using FilteredRelation failed on empty result

Reported by: Dmitrii Azarenko Owned by: Can Sarıgöl
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: ORM FilteredRelation
Cc: Can Sarıgöl Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Dmitrii Azarenko)

When I try to join table with using FilteredRelation clause it failed if the result of a joined table is null.
This error can be reproduce by adding the following test case to the corresponding test module:


def test_select_related_empty_join(self):
            empty_join=FilteredRelation('book', condition=Q(
                book__title='not existing book')),


ERROR: test_select_related_empty_join (filtered_relation.tests.FilteredRelationTests)
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/", line 59, in testPartExecutor
  File "/usr/lib/python3.6/unittest/", line 605, in run
  File "/home/adv/projects/github/django/tests/filtered_relation/", line 46, in test_select_related_empty_join
  File "/usr/lib/python3.6/unittest/", line 674, in assertFalse
    if expr:
  File "/home/adv/projects/github/django/django/db/models/", line 271, in __bool__
  File "/home/adv/projects/github/django/django/db/models/", line 1232, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/adv/projects/github/django/django/db/models/", line 67, in __iter__
    rel_populator.populate(row, obj)
  File "/home/adv/projects/github/django/django/db/models/", line 1876, in populate
    self.local_setter(from_obj, obj)
  File "/home/adv/projects/github/django/django/db/models/sql/", line 892, in local_setter
    f.remote_field.set_cached_value(from_obj, obj)
  File "/home/adv/projects/github/django/django/db/models/fields/", line 23, in set_cached_value
    instance._state.fields_cache[self.get_cache_name()] = value
AttributeError: 'NoneType' object has no attribute '_state'


This bug is typical since version 2.0 and exists until now.

Change History (11)

comment:1 by Dmitrii Azarenko, 6 years ago

Description: modified (diff)

comment:2 by Tim Graham, 6 years ago

Triage Stage: UnreviewedAccepted
Version 0, edited 6 years ago by Tim Graham (next)

comment:3 by Can Sarıgöl, 5 years ago

Hi, could you review this PR.

Last edited 5 years ago by Can Sarıgöl (previous) (diff)

comment:4 by Can Sarıgöl, 5 years ago

Cc: Can Sarıgöl added
Has patch: set

comment:5 by Tim Graham, 5 years ago

Patch needs improvement: set

Tests aren't passing.

comment:6 by Can Sarıgöl, 5 years ago

Sorry, I didn't think it was about PR. I'm looking at

comment:7 by Can Sarıgöl, 5 years ago

Patch needs improvement: unset

I hope the fails are not about me this time :)

comment:8 by Can Sarıgöl, 5 years ago

Owner: changed from nobody to Can Sarıgöl
Status: newassigned

comment:9 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set
Version: 2.0master

comment:10 by Can Sarıgöl, 5 years ago

Patch needs improvement: unset

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In c00b4dbd:

Fixed #29810 -- Fixed crash of select_related() on FilteredRelation with empty result.

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