antonio_antuan antonio_antuan - 7 months ago 72
Python Question

How to replace columns in sqlalchemy query

I have the query:

q = Session.query(func.array_agg(Order.col))


The compiled query will be:

SELECT array_agg(order.col) FROM orders


I want dynamically replace the existing column. After replacing query have to be:

SELECT group_concat(orders.col) FROM orders


I have to use Session and model. I don't have to use SQLAlchemy core. I don't have to use subqueries. And, of course, there can be some other columns, but I need to replace only one. I tried to replace objects in
column_descriptions
property, I tried to use
q.selectable.replace
(or something like this, sorry, but I don't remember right names) and I didn't get right result.

Answer

The right method:

q = Session.query(func.array_agg(Order.col)) 

q.with_entities(func.group_concat(Order.col))

SELECT group_concat(orders.col) FROM orders
Comments