Is there support for the IN statement in the SQL Expression language used in SQLAlchemy?
Can a query like the one below be expressed in the "SQL Expression Language" used by 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, like my examples below, or in some 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])
+12
a source to share
2 answers
select([foo], foo.c.bar.in_([1, 2, 3]))
You can use the method .in_()
using Instrumented columns or attributes. Both work.
This is mentioned here in the first SQLAlchemy tutorial.
+20
a source to share
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_
Usage example:
ids_to_select = ["1", "2", "3"]
query(Model).filter(Model.id.in_(ids_to_select)).all()
+4
a source to share