A query with a function in the Flask-SQLAlchemy model gives a BaseQuery an undetectable error

I want to request services between two dates and sum their prices. When I try to use func.sumwith Services.query, I get TypeError: BaseQuery object is not callable. How can I execute a query using a function using Flask-SQLAlchemy?

Services.query(func.sum(Services.price)).filter(Services.dateAdd.between(start, end))
+4
source share
1 answer

Model.queryis a shortcut to db.session.query(Model), it cannot be called. If you are not requesting a model, continue to use db.session.query(...)it as you would with regular SQLAlchemy.

db.session.query(db.func.sum(Services.price)).filter(Services.dateAdd.between(start, end))
+6
source

Source: https://habr.com/ru/post/1662568/


All Articles