Opened 17 years ago

Closed 17 years ago

Last modified 16 years ago

#2080 closed defect (duplicate)

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

Reported by: Antti Kaihola Owned by: Adrian Holovaty
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: no UI/UX: no

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 Antti Kaihola 17 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 Antti Kaihola 17 years ago.
Added testing with alternate syntax and checking what kind of join Django uses

Download all attachments as: .zip

Change History (9)

Changed 17 years ago by Antti Kaihola

Attachment: m2o_or_lookup_test.diff added

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

comment:1 Changed 17 years ago by Antti Kaihola

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 17 years ago by Antti Kaihola

Also #1801 resembles this a bit.

Changed 17 years ago by Antti Kaihola

Attachment: m2o_or_lookup_test.2.diff added

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

comment:3 Changed 17 years ago by mir@…

Cc: mir@… added

comment:4 Changed 17 years ago by mir@…

Cc: mir@… added; mir@… removed

comment:5 Changed 17 years ago by Adrian Holovaty

Resolution: duplicate
Status: newclosed

Duplicate of #1801.

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

Cc: mattimustang@… added

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

Cc: yannvr@… added
Note: See TracTickets for help on using tickets.
Back to Top