python - How to filter on calculated column of a query and meanwhile preserve mapped entities -
i have query selects entity , calculated fields
q = session.query(recipe,func.avg(recipe.somefield).join(.....)
i use select in way assumes can subscript result "recipe" string:
for entry in q.all(): recipe=entry.recipe # access keyedtuple recipe attribute ...
now need wrap query in additional select, filter calculated field avg:
q=q.subquery(); q=session.query(q).filter(q.c.avg_1 > 1)
and cannot access entry.recipe anymore! there way make sqlalchemy adapt query enclosing one, aliased(adapt_on_names=true) or
select_from_entity()`? tried using given error
as michael bayer mentioned in relevant google group thread, such adaptation done via query.from_self()
method. problem in case didn't know how refer column want filter on
this due fact, calculated i.e. there no table refer to!
i might resort using literals(.filter('avg_1>10')
), 'd prefer stay in more orm-style
so, came - explicit column expression
row_number_column = func.row_number().over( partition_by=recipe.id ).label('row_number') query = query.add_column( row_number_column ) query = query.from_self().filter(row_number_column == 1)
Comments
Post a Comment