Manatax
Manatax

Reputation: 4213

SQLAlchemy how to use OR on filter

I'm trying to use an OR on this query:

User.query.filter(or_(username==data['username'], email==data['email'])).first()

But, I get this message:

global name 'or_' is not defined

I'm using SqlAlchemy 0.9.7 (it already includes or_)

Anyone encountered this before and/or knows how to fix it?

Upvotes: 0

Views: 1324

Answers (1)

Nuchimik
Nuchimik

Reputation: 199

Most likely you did not import the necessary module

from sqlalchemy import or_

Also, I'm not sure if this operator can be used with a filter. But try this approach.

The or_() conjunction is also available using the Python | operator (though note that compound expressions need to be parenthesized in order to function with Python operator precedence behavior):

User.query.filter((username==data['username']) | (email==data['email'])).first()

Upvotes: 2

Related Questions