sqlalchemy exists for query sqlalchemy exists for query python python

sqlalchemy exists for query


The following solution is a bit simpler:

from sqlalchemy.sql import existsprint session.query(exists().where(User.email == '...')).scalar()


The most acceptable and readable option for me is

session.query(<Exists instance>).scalar()

like

session.query(User.query.filter(User.id == 1).exists()).scalar()

which returns True or False.


There is no way that I know of to do this using the orm query api. But you can drop to a level lower and use exists from sqlalchemy.sql.expression:

from sqlalchemy.sql.expression import select, existsusers_exists_select = select((exists(users_query.statement),)) print engine.execute(users_exists_select).scalar()