The problem
In PostgreSQL, checking whether a field is in a given list is done using the IN
operator:
SELECT * FROM stars WHERE star_type IN ('Nova', 'Planet');
What is the SQLAlchemy equivalent for an IN
SQL query?
What have I tried
Python's in
db_session.query(Star).filter(Star.star_type in ('Nova', 'Planet'))
The query returns empty.
SQLAlchemy's or_
db_session.query(Star).\
filter(or_(
Star.star_type == 'Nova',
Star.star_type == 'Planet'
))
This query returns the right result, but it is not elegant and hard to expand.
You can do it like this: