Oladapo Adebowale
Oladapo Adebowale

Reputation: 59

How to check the email and password field using SQLAlchemy

I want to check if a row with two values for two columns exists. I tried the following code, but it only works for the first column, email, but not pass. How do I check both columns in the where clause?

session.query(exists().where(User.email == '...' and User.pass == '...')).scalar()

Upvotes: 2

Views: 1276

Answers (1)

davidism
davidism

Reputation: 127200

SQLAlchemy filter objects are combined with the bitwise operators & and |, not the boolean and and or.

session.query((exists().where(User.email == '...') & (User.password == '...'))).scalar()

There are also and_ and or_ functions to combine multiple statements.

import sqlalchemy as sa

sa.and_(User.email == '...', User.password == '...')

Upvotes: 1

Related Questions