Opened 17 years ago

Closed 16 years ago

#7330 closed (duplicate)

Filtering on multiple indirect relations to same table gives incorrect SQL

Reported by: Pavel Anossov Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: qsrf, join
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I came across the following problem: if you filter through two foreign keys to same table, it gets referenced only once, like this:

class C(model):
    somefield = IntegerField()

# Every B is related to a C
class B(model):
    c = ForeignKey(C)

# Every A is related (differently) to two Bs, and indirectly to two different Cs
class A(model):
    b1 = ForeignKey(B, db_column='b1')
    b2 = ForeignKey(B, db_column='b2')

A.objects.filter(b1__c = 1).filter(b2__c = 2).query.as_sql()

# SELECT ...
# FROM `A`
# INNER JOIN `B` ON (`A`.`b1` = `B`.`id`)
# INNER JOIN `B` T4 ON (`A`.`b2` = T4.`id`)
# WHERE `B`.`c_id` = 1  AND `B`.`c_id` = 2
#
# This is wrong, it should be
# SELECT ...
# FROM `A`
# INNER JOIN `B` ON (`A`.`b1` = `B`.`id`)
# INNER JOIN `B` T4 ON (`A`.`b2` = T4.`id`)
# WHERE `B`.`c_id` = 1  AND `T4`.`c_id` = 2

# If we filter deeper, it gets more obvious:
A.objects.filter(b1__c__somefield = 1).filter(b2__c__somefield = 2).query.as_sql()

# SELECT ...
# FROM `A`
# INNER JOIN `B` ON (`A`.`b1` = `B`.`id`)
# INNER JOIN `C` ON (`B`.`c_id` = `C`.`id`)
# INNER JOIN `B` T4 ON (`A`.`b2` = T4.`id`)
# WHERE `C`.`somefield` = 1  AND `C`.`somefield` = 2
#
# This is wrong, should be
# SELECT ...
# FROM `A`
# INNER JOIN `B` ON (`A`.`b1` = `B`.`id`)
# INNER JOIN `C` ON (`B`.`c_id` = `C`.`id`)
# INNER JOIN `B` T4 ON (`A`.`b2` = T4.`id`)
# INNER JOIN `C` T5 ON (`T4`.`c_id` = `T5`.`id`)
# WHERE `C`.`somefield` = 1  AND `T5`.`somefield` = 2

Suggested solution is to call sql.query.join in sql.query.setup_joins with always_create=dupe_multis not only in m2m fields, but in o2o and m2o fields as well (see patch). I'm not sure if it's not breaking anything though, I should test it.

Attachments (1)

query.py.diff (648 bytes ) - added by Pavel Anossov 17 years ago.

Download all attachments as: .zip

Change History (4)

by Pavel Anossov, 17 years ago

Attachment: query.py.diff added

comment:1 by Pavel Anossov, 17 years ago

Has patch: unset

Patch certainly breaks filters, so never mind it.. yet the problem exists.

comment:2 by oyvind, 16 years ago

Same as #7277 , #7125

comment:3 by Jacob, 16 years ago

Resolution: duplicate
Status: newclosed

I don't think this is related to #7277, but it is indeed a duplicate of #7125.

Note: See TracTickets for help on using tickets.
Back to Top