Reputation: 34251
Is it possible to express a query like the one below in the "SQL Expression Language" used in SQLAlchemy?
SELECT * FROM foo WHERE foo.bar IN (1,2,3)
I want to avoid writing the WHERE-clause in plain text. Is there a way to express this similar to my examples below or in any way that doesn't use plain text?
select([foo], in(foo.c.bar, [1, 2, 3]))
select([foo]).in(foo.c.bar, [1, 2, 3])
Upvotes: 20
Views: 15064
Reputation: 222862
select([foo], foo.c.bar.in_([1, 2, 3]))
You can use the .in_()
method with Columns or with Instrumented attributes. Both work.
It is mentioned here on SQLAlchemy's first tutorial.
Upvotes: 32
Reputation: 8528
The .in_() operator now lives in the ColumnOperators class, documented @ http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.in_
Example usage:
ids_to_select = ["1", "2", "3"]
query(Model).filter(Model.id.in_(ids_to_select)).all()
Upvotes: 9