A union of two dynamic queries on the same table returns results of only one of the unioned queries

Issue #2207 resolved
Former user created an issue

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)

  1. Former user Account Deleted

    This behavior is observed in both versions 0.7.1 and 0.6.9 on MySQL and sqlite.

  2. Mike Bayer repo owner
    • changed component to orm
    • changed milestone to 0.6.9
    • assigned issue to

    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.

  3. Former user 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.

  4. Log in to comment