I have a bunch of tables in my database, that are all linked together in the same way. I need to run queries against that bunch of tables, but I would like to be able to figure out on the subset of those tables at runtime. To do the select, I need to list my tables in the select
statement and then reference them in the join
clauses:
stmt = sa.select(
TableOne,
TableTwo,
TableThree
).join(
TableTwo,
onclause=TableOne.id==TableTwo.parent,
isouter=True
).join(...)
I would like to build that request dynamically. Like:
stmt = select(TableOne)
# somewhere else, like an `_join_with_another_table` function:
stmt = stmt.extend_select(TableTwo).join(TableTwo, onclause=..., isouter=...)
I can add more join
clauses, but that leaves me with a select that does not have the fields from the joined table, so I need to add that table to the select part.
Adding another .select
makes a nested query
Is there a way to do that sort of thing?