SQLAlchemy: create an intentionally empty query?

python, sqlalchemy

Solution

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())`

Problem

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()`.

Original source