On 4/24/15 5:25 PM, Пайков Юрий wrote:
q = session.query(Recipe, func.avg(Recipe.field1).over(...)).join(...)
I have a query which selects some mapped entity as well as other columns.
I then refer to the name of that entity when working with the result of the query:
for entry in q.all():
recipe=entry.Recipe
  ....
Now, I want to add filtering by some calculated criteria to my query, and so I wrap it in an additional query:
q = q.subquery();
q = session.query(q).filter(q.c.avg_1 > 10 )
However, this way I can no longer access entry.Recipe! Is there a way to make sqlalchemy adapt names? I tried aliased and select_entity_from, but no luck :(

this is getting into less reliable stuff, but instead of subquery() -> session.query(q), use the from_self() method. It's designed to work this way, and your Recipe entity will be adapted into the subquery.

I've observed that the vast majority of my users don't seem to get into queries like these, so from_self() is not as popular (or widely tested) as it should be, but it is at the base of a lot of widely used functions like count() and subquery eager loading, so give it a try:

http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html?highlight=from_self#sqlalchemy.orm.query.Query.from_self

apparently it needs some documentation too :)


--
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 <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to