I have a two-table hierarchical setup where table A references table B, which then references back to a different record in table A, and so on... but only up to a given recursion depth.
I have this working well using SQLAlchemy and declarative. I'm also successfully using eager loading with the lazy
and join_depth
properties on the table relations. This is as per the SQLAlchemy documentation.
However, this arrangement fixes the recursion depth at 'join_depth
' once at program load time... but with the data I am using I know the recursion depth I should be using each time. How can I change the recursion depth used on a per-query basis?
I've considered fiddling with the master join_depth
property on the base ORM object, but this won't work since I've got a multi-threaded scoped_session application where that would be dangerous (not to mention the fact that the parameter is pretty hard to locate within SQLAlchemy at runtime!).
I've also looked at using joinedload
with the query but don't see how to vary the depth with that.
I'm also aware of the 'WITH RECURSIVE
' SQL syntax available in some databases via CTEs, but as great as it is, I want to avoid that for the moment since some DBs still don't support it (and neither does SQLAlchemy - at least not at the moment and not without a lot of dialect customization).