SQLAlchemy: Querying compound primary key with `IN` operator
Asked Answered
G

1

7

Assuming a table foo with compound primary key (a,b), How I can generate following sql query with SQLAlchemy (postgresql dialect)?

SELECT * FROM foo WHERE (a,b) IN ((1,2), (2,3));
Goodrow answered 18/3, 2014 at 13:56 Comment(0)
G
14

Here is the answer:

from sqlalchemy.sql.expression import Tuple
session.query(Foo).filter(Tuple(Foo.a, Foo.b).in_([(1,2), (3,4)])).all()
Goodrow answered 18/3, 2014 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.