mirror of
https://github.com/django/django.git
synced 2025-10-29 16:46:11 +00:00
Fixed #17886 -- Fixed join promotion in ORed nullable queries
The ORM generated a query with INNER JOIN instead of LEFT OUTER JOIN in a somewhat complicated case. The main issue was that there was a chain of nullable FK -> non-nullble FK, and the join promotion logic didn't see the need to promote the non-nullable FK even if the previous nullable FK was already promoted to LOUTER JOIN. This resulted in a query like a LOUTER b INNER c, which incorrectly prunes results.
This commit is contained in:
@@ -910,7 +910,12 @@ class Query(object):
|
||||
# Not all tables need to be joined to anything. No join type
|
||||
# means the later columns are ignored.
|
||||
join_type = None
|
||||
elif promote or outer_if_first:
|
||||
elif (promote or outer_if_first
|
||||
or self.alias_map[lhs].join_type == self.LOUTER):
|
||||
# We need to use LOUTER join if asked by promote or outer_if_first,
|
||||
# or if the LHS table is left-joined in the query. Adding inner join
|
||||
# to an existing outer join effectively cancels the effect of the
|
||||
# outer join.
|
||||
join_type = self.LOUTER
|
||||
else:
|
||||
join_type = self.INNER
|
||||
|
||||
Reference in New Issue
Block a user