What's the best way to create an intentionally empty query in SQLAlchemy?
For example, I've got a few functions which build up the query (adding WHERE
clauses, for example), and at some points I know that the the result will be empty.
What's the best way to create a query that won't return any rows? Something like Django's QuerySet.none()
.
Compare result with None is ok if you want to know if its empty.
The python package SQLAlchemy was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.
The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.
The Engine is the starting point for any SQLAlchemy application. It's “home base” for the actual database and its DBAPI, delivered to the SQLAlchemy application through a connection pool and a Dialect , which describes how to talk to a specific kind of database/DBAPI combination.
If you need the proper return type, just return session.query(MyObject).filter(sqlalchemy.sql.false())
.
When evaluated, this will still hit the DB, but it should be fast.
If you don't have an ORM class to "query", you can use false()
for that as well:
session.query(sqlalchemy.false()).filter(sqlalchemy.false())
db_session(YourModel).query.filter(False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With