Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying with function on Flask-SQLAlchemy model gives BaseQuery object is not callable error

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

Services.query(func.sum(Services.price)).filter(Services.dateAdd.between(start, end)) 
like image 478
Christopher Nelson Avatar asked Dec 01 '16 19:12

Christopher Nelson


People also ask

What does DB Drop_all () do?

You delete everything in the database using the db. drop_all() function to add the tags and post_tag tables safely and to avoid any of the common issues related to adding new tables to a database. Then you create all the tables anew using the db. create_all() function.

How do I select a column in SQLAlchemy?

SQLAlchemy Core The already created students table is referred which contains 4 columns, namely, first_name, last_name, course, score. But we will be only selecting a specific column. In the example, we have referred to the first_name and last_name columns. Other columns can also be provided in the entities list.

Which of the following extensions is used to integrate a flask application with SQLAlchemy?

Flask-SQLAlchemy is the Flask extension that adds support for SQLAlchemy to your Flask application. What is ORM (Object Relation Mapping)? Most programming language platforms are object oriented.

What does DB SQLAlchemy app do?

SQLAlchemy is an SQL toolkit that provides efficient and high-performing database access for relational databases. It provides ways to interact with several database engines such as SQLite, MySQL, and PostgreSQL.


1 Answers

Model.query is a shortcut to db.session.query(Model), it's not callable. If you're not querying a model, continue to use db.session.query(...) as you would with regular SQLAlchemy.

db.session.query(db.func.sum(Services.price)).filter(     Services.dateAdd.between(start, end) ) 
like image 150
davidism Avatar answered Sep 20 '22 01:09

davidism