0

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?

4
  • 2
    Did you from sqlalchemy.sql.expression import or_? Commented Sep 26, 2017 at 1:08
  • import flask.ext.sqlalchemy Commented Sep 26, 2017 at 1:11
  • @thaavik individually importing the method fixed that issue. Commented Sep 26, 2017 at 1:49
  • How is this "out of topic"? Commented Oct 6, 2017 at 18:58

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

individually importing the method fixed that issue, although I don't know why it would not be included with the entire library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.