Opened 18 years ago

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

Download all attachments as: .zip

Change History (9)

by Antti Kaihola, 18 years ago

Attachment: m2o_or_lookup_test.diff added

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

comment:1 by Antti Kaihola, 18 years ago

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

Also #1801 resembles this a bit.

by Antti Kaihola, 18 years ago

Attachment: m2o_or_lookup_test.2.diff added

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

comment:3 by mir@…, 18 years ago

Cc: mir@… added

comment:4 by mir@…, 18 years ago

Cc: mir@… added; mir@… removed

comment:5 by Adrian Holovaty, 18 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #1801.

comment:6 by Matthew Flanagan <mattimustang@…>, 17 years ago

Cc: mattimustang@… added

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

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