0

I have some sql queries I'm trying to run as sqlalchemy.orm.query objects and I'm wondering if there is a way to use OR. I can use AND by adding commas into the filter statement but I don't know how to do an OR. Example code below:

query = MyTable.q.filter(MyTable.id == some_number)

However I don't know how to do a query with an OR

query = MyTable.q.filter(MyTable.id == some_number OR MyTable.id == some_other_number)

Any help is greatly appreciated

1

1 Answer 1

1
from sqlalchemy.sql import or_

query = MyTable.q.filter(
        or_(MyTable.id == some_number, MyTable.id == some_other_number)
    )

Of course, there's no point in this case since you can solve it with in_.

query = MyTable.q.filter(MyTable.id.in_([some_number, some_other_number])
Sign up to request clarification or add additional context in comments.

Comments

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.