Using OR in SQLAlchemy Using OR in SQLAlchemy python python

Using OR in SQLAlchemy

From the tutorial:

from sqlalchemy import or_filter(or_( == 'ed', == 'wendy'))

SQLAlchemy overloads the bitwise operators &, | and ~ so instead of the ugly and hard-to-read prefix syntax with or_() and and_() (like in Bastien's answer) you can use these operators:

.filter((AddressBook.lastname == 'bulger') | (AddressBook.firstname == 'whitey'))

Note that the parentheses are not optional due to the precedence of the bitwise operators.

So your whole query could look like this:

addr = session.query(AddressBook) \    .filter( == "boston") \    .filter((AddressBook.lastname == 'bulger') | (AddressBook.firstname == 'whitey'))

or_() function can be useful in case of unknown number of OR query components.

For example, let's assume that we are creating a REST service with few optional filters, that should return record if any of filters return true. On the other side, if parameter was not defined in a request, our query shouldn't change. Without or_() function we must do something like this:

query = Book.queryif filter.title and    query = query.filter((Book.title.ilike(filter.title))|( if filter.title:    query = query.filter(Book.title.ilike(filter.title))else if    query = query.filter(

With or_() function it can be rewritten to:

query = Book.querynot_null_filters = []if filter.title:    not_null_filters.append(Book.title.ilike(filter.title))if    not_null_filters.append( len(not_null_filters) > 0:    query = query.filter(or_(*not_null_filters))