I am trying to construct a filter against a table that has two references
to the same child table. But I am completely confused how to go about it.
I've seen lots of examples for using aliases, but I have not been able to
get any to work. So I'll just try to give my example simply, and ask what
is a possible way to achieve what I want.
Tables:
class Child():
__table__ = 'child'
child_key = Column(Integer, primary_key=True)
attr = Column(String)
class Parent():
__table__ = 'parent'
parent_key = Column(Integer, primary_key=True)
child_key1 = Column(Integer, ForeignKey('child.child_key'))
child_key2 = Column(Integer, ForeignKey('child.child_key'))
child1 = relationship('Child',
primaryjoin='Child.child_key==Parent.child_key1',
uselist=False)
child2 = relationship('Child',
primaryjoin='Child.child_key==Parent.child_key=2',
uselist=False)
Now, I want to filter parents by some attribute (attr) of either child1 or
child2. Naively, I would want a filter like: query.filter(
or_(child1.attr=='attribute', child2.attr=='attribute')), but I have no
idea how to reference both child1 and child2 after joining them.
Here is how I typically join all my relationships for querying (I prefer
this syntax, because it uses the relationship definition for the join):
query = Parent.query.join(Parent.child1).outerjoin(Parent.child2)
It's at this point I am stuck. I'm not sure how to ask for an attribute in
'either child1 or child2'. Also, this may not be important, but the second
join is outer, because child2 can be null.
Any suggestions?
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.