I gave up on attempts to do something similar a while back, because it became to problematic to examine all the SqlAlchemy objects – and the existing query – in an effort to construct the joins and query correctly.
I would up using a two-phase approach. phase 1 analyzes the 'requested metrics' to figure out which tables and columns are needed, and raises an error if things look bad. phase 2 generates the query. I use a python dict to store metadata about the query as it is analyzed, using the tables as keys and building an array of the columns - this way i only join the table once. based on what tables are needed in the dict, or other data on the metrics I pre-calculate, i know how to structure the joins. this approach is somewhat restricting, but works very well, is quick to deploy and easy to maintain. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.