There are some of methods of Query object that without delay trouble SQL and go back a cost containing loaded database outcomes.
Here’s a short rundown of returning list and scalars −
all()
It returns a listing. Given below is the line of code for all() function.
session.query(Customers).all()
Python console presentations following SQL expression emitted −
SELECT customers.id
AS customers_id, customers.name
AS customers_name, customers.address
AS customers_address, customers.email
AS customers_email
FROM customers
first()
It applies a restrict of 1 and returns the primary result as a scalar.
SELECT customers.id
AS customers_id, customers.name
AS customers_name, customers.address
AS customers_address, customers.email
AS customers_email
FROM customers
LIMIT ? OFFSET ?
The sure parameters for LIMIT is 1 and for OFFSET is zero.
One()
This command fully fetches all rows, and if there is not exactly one object identity or composite row present in the result, it raises an mistakes.
session.query(Customers).one()
With more than one rows discovered −
MultipleResultsFound: Multiple rows were found for one()
With no rows determined −
NoResultFound: No row was found for one()
The one() approach is beneficial for systems that assume to deal with “no objects determined” versus “more than one gadgets determined” in a different way.
scalar()
It invokes the one() technique, and upon fulfillment returns the first column of the row as follows −
session.query(Customers).filter(Customers.id == 3).scalar()
This generates following SQL statement −
SELECT customers.id
AS customers_id, customers.name
AS customers_name, customers.address
AS customers_address, customers.email
AS customers_email
FROM customers
WHERE customers.id = ?