I use the following pattern in my REST APIs, building up the select, joins, where conditions, group bys, order bys, depending on the query parameters passed in by the user:
selects = [Foo]
joins = [(Bar,
Foo.c.id == Bar.c.foo_id)]
where_conditions = [
Foo.c.id == request.args['pk']]
if request.args.get('include_baz'):
selects.append(Baz)
joins.append((Baz,
Bar.c.id == Baz.c.bar_id))
What I would like to do is the following:
sel = select(
*selects
).join(
*joins # doesn't work
).where(
*where_conditions
)
This works for everything except for `join` and `outerjoin`. So I have to write it like this:
sel = select(*selects)
for table, condition in joins:
sel = sel.join(table, condition)
sel = se.where(*where_conditions)
Is there some way to perform a join by passing an array of (table, conditions) so I can write the SQL without all of the `sel = sel. ` noise?
What I've been doing is using a function like the following:
def collection_query(selects, joins, where_conditions, ...)
But this has other problems and I would like to go back to raw sqlalchemy.
Thanks and best regards,
Matthew