sedavidw
sedavidw

Reputation: 11691

Using OR in SQLAlchemy ORM query

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

Upvotes: 0

Views: 534

Answers (1)

davidism
davidism

Reputation: 127180

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])

Upvotes: 1

Related Questions