﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29810	Left outer join using FilteredRelation failed on empty result	Dmitrii Azarenko	Can Sarıgöl	"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 [https://github.com/django/django/blob/2.0/tests/filtered_relation/tests.py#L9 test module]:
{{{#!div style=""font-size: 80%""
tests.filtered_relation.tests.FilteredRelationTests:
  {{{#!python
def test_select_related_empty_join(self):
    self.assertFalse(
        Author.objects.annotate(
            empty_join=FilteredRelation('book', condition=Q(
                book__title='not existing book')),
        ).select_related('empty_join')
    )
}}}
}}}
{{{#!div style=""font-size: 80%""
Error:
{{{
............E.s................
======================================================================
ERROR: test_select_related_empty_join (filtered_relation.tests.FilteredRelationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/usr/lib/python3.6/unittest/case.py"", line 59, in testPartExecutor
    yield
  File ""/usr/lib/python3.6/unittest/case.py"", line 605, in run
    testMethod()
  File ""/home/adv/projects/github/django/tests/filtered_relation/tests.py"", line 46, in test_select_related_empty_join
    ).select_related('empty_join')
  File ""/usr/lib/python3.6/unittest/case.py"", line 674, in assertFalse
    if expr:
  File ""/home/adv/projects/github/django/django/db/models/query.py"", line 271, in __bool__
    self._fetch_all()
  File ""/home/adv/projects/github/django/django/db/models/query.py"", line 1232, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/home/adv/projects/github/django/django/db/models/query.py"", line 67, in __iter__
    rel_populator.populate(row, obj)
  File ""/home/adv/projects/github/django/django/db/models/query.py"", line 1876, in populate
    self.local_setter(from_obj, obj)
  File ""/home/adv/projects/github/django/django/db/models/sql/compiler.py"", line 892, in local_setter
    f.remote_field.set_cached_value(from_obj, obj)
  File ""/home/adv/projects/github/django/django/db/models/fields/mixins.py"", 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."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	ORM FilteredRelation	Can Sarıgöl	Accepted	1	0	0	0	0	0
