I'm using the declarative base and filter my searches like this :
MyModel.query.filter(MyModel.id == 5)
But I need to obtain the 5 using a complex query involving many joins.
Is there a way to do the following in SQLAlchemy:
MyModel.query.filter(MyModel.id == text('SELECT a.id FROM accounts a LEFT JOIN sessions s ON s.account_id = a.id LEFT JOIN suitable t ON t.session_id = s.id'))
BUT, There is a twist:
I know I can just do the first query, get the resulting id, and then call the MyModel.query.filter(MyModel.id == result).
What I'm looking at is a way to tell SQLAlchemy to generate a query such as:
SELECT ... from MyModel WHERE MyModel.id = (SELECT a.id FROM accounts a LEFT JOIN sessions s ON s.account_id = a.id LEFT JOIN suitable t ON t.session_id = s.id)
In order to only have only one query executed, instead of two.