Code

Opened 8 years ago

Closed 8 years ago

Last modified 6 years ago

#2080 closed defect (duplicate)

OR lookup across many-to-one relationships gives unexpected results

Reported by: akaihola Owned by: adrian
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: mir@…, mattimustang@…, yannvr@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

If

  • model B has a foreign key on model A,
  • there is an a1 = A(field1=value1) object but no related B objects

and you do

A.objects.filter(Q(field1=value1) | Q(b__field2=value2))

the result will fail to include a1.

This is quite unexpected in my opinion, and I think the reason is that the resulting SQL query uses an inner join instead of an outer join.

Attachments (2)

m2o_or_lookup_test.diff (1.3 KB) - added by akaihola 8 years ago.
a test case for an OR lookup across a foreign key relation
m2o_or_lookup_test.2.diff (2.1 KB) - added by akaihola 8 years ago.
Added testing with alternate syntax and checking what kind of join Django uses

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by akaihola

a test case for an OR lookup across a foreign key relation

comment:1 Changed 8 years ago by akaihola

This might be the same issue as in ticket #1535, although that seems to be already fixed but it doesn't fix this.

comment:2 Changed 8 years ago by akaihola

Also #1801 resembles this a bit.

Changed 8 years ago by akaihola

Added testing with alternate syntax and checking what kind of join Django uses

comment:3 Changed 8 years ago by mir@…

  • Cc mir@… added

comment:4 Changed 8 years ago by mir@…

  • Cc mir@… added; mir@… removed

comment:5 Changed 8 years ago by adrian

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

Duplicate of #1801.

comment:6 Changed 7 years ago by Matthew Flanagan <mattimustang@…>

  • Cc mattimustang@… added

comment:7 Changed 6 years ago by Yopi <yannvr@…>

  • Cc yannvr@… added

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.