- attached sqlalchemy_bug.py
A union of two dynamic queries on the same table returns results of only one of the unioned queries
With a parent, a child and an association for a many to many relationship, this type of query returns incorrect results where 'children' is defined as a 'dynamic' relationship:
p1 = Parent.get(1), p2 = Parent.get(2)
returns only p2's children
p1.children.union(p2.children)
A more detailed testcase is attached.
Comments (6)
-
Account Deleted -
Account Deleted This behavior is observed in both versions 0.7.1 and 0.6.9 on MySQL and sqlite.
-
repo owner some similar tests:
q1 = session.query(B).filter(with_parent(a1, 'bs')) q2 = session.query(B).filter(with_parent(a2, 'bs')) print q1.union(q2).all() session.query(B).filter(or_(with_parent(a1, 'bs'), with_parent(a2, 'bs'))).distinct().all()
the other direction , i.e.:
print session.query(A).filter(A.bs.contains(b1)).union( session.query(A).filter(A.bs.contains(b2)) ).all()
works, because
LazyLoader.lazy_clause()
produces the "reverse" clause each time, whereas the "forwards" version relies upon the created-just-once version.So this is good, by identifying that re-using the "with parent" lazy clause has a dupe bindparam bug, we can fix that bug and then safely re-use the more common "forwards" direction comparison and remove repeated calls to
_create_lazy_clause
.needs tests but the attached patch passes all tests.
-
repo owner - changed status to resolved
5b8738b7e390471bd31b9ece90d0a3fd7653d859 edf3ccca2a7e55d24a2e69cb36206bd63f453435
thanks for the test !
-
Account Deleted Just as a side note: I applied the patch to a local install to check our production use case that uncovered this bug and it now works correctly with the patch applied.
Thanks for the very quick response and patch! Looking forward to seeing it land in the next release.
-
repo owner - removed milestone
Removing milestone: 0.6.9 (automated comment)
- Log in to comment
Test case for reproducing the bug