Say I have a class like this:
class Foo(declarativeBase): bars1 = relationship(Bar.Bar, secondary=foos_to_bars1, collection_class=set()) bars2 = relationship(Bar.Bar, secondary=foos_to_bars2, collection_class=list())
(Each of the relationships gives me a "Bar" with certain conditions). At some point, I want to get instances of βFooβ that have a βbarβ (an instance of Bar.Bar) in any of the relationships.
If I try to do:
def inAnyBar(bar) query(Foo).filter(or_(Foo.bars1.contains(bar), Foo.bars2.contains(bar)).all()
I get an empty result.
It looks (to me) as if I am doing something like:
query(Foo).join(Foo.bars1).filter(Foo.bars1.contains(bar)).\ join(Foo.bars2).filter(Foo.bars1.contains(bar))
Since Foo.bars1 does not contain a bar, the second filter gives empty results.
I managed to find a workaround with subqueries (each joins + a filter in the subquery, then or all the subqueries), but I would like to know if there is a better way to do this ...
I found this: http://techspot.zzzeek.org/2008/09/09/selecting-booleans/
This does what I want to do, but it is for SqlAlchemy 0.5, and I am (almost) sure that there is a "cleaner" way to do it with SqlAlchemy 0.6.6
Thanks!
source share