On Thu, Mar 28, 2019 at 4:43 PM Ian Miller <[email protected]> wrote: > > Hello all, > > I am in the process of trying to create a dynamic expression query engine in > an application I'm working on. > > So there is a formula that gets defined like so: > > formula = '"metric:123" + "metric:456" + "metric:789"' > > Each metric maps to a column in the database tables - long story short, I'm > able to retrieve the metric by ID, and instantiate an InstrumentedAttribute > object that has the SQLAlchemy metadata for the associated column. What I'm > trying to achieve is to be able to iterate through the formula, and > dynamically build a SQLALchemy query that maps to the formula.
this seems a bit vague, "instantiate an InstrumentedAttribute" is not exactly a public SQLAlchemy API, these are created internally as part of the mapping process, so I wouldn't know exactly how you're going about "instantiating" these, not like it isn't possible but normally if youre generating dynamic SQL you'd just be building with column() objects. > > For example, the formula defined above would look something like this in SQL: > > SELECT post.id + campaign.id + asset.id > FROM post, campaign, asset > WHERE ......; > > The idea is to translate the above to something like: > > session.query(<sqlalchemy.orm.attributes.InstrumentedAttribute object at > 0x7ff9269f92b0> + <sqlalchemy.orm.attributes.InstrumentedAttribute object at > 0x7ff9269c5990> + <sqlalchemy.orm.attributes.InstrumentedAttribute object at > 0x7ff926896048>).all() this also puzzles me. you're saying you have a list of InstrumentedAttribute objects. so...the above would be, "session.query(sum(my_list_of_attributes[1:], my_list_of_attributes[0])).all()" . or something similar just applying the + operator to the items. is that what you're looking for ? > > I've tried a couple of approaches of dynamically generating the SQLAlchemy > ORM query, but I haven't been able to find anything that works. Would anyone > have any idea or tips on how to accomplish this? > > Thank you! > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
