postgresql - SQLAlchemy:使用 `IN` 运算符查询复合主键

标签 postgresql sqlalchemy compound-key

假设一个表 foo 具有复合主键 (a,b),我如何使用 SQLAlchemy(postgresql 方言)生成以下 sql 查询?

SELECT * FROM foo WHERE (a,b) IN ((1,2), (2,3));

最佳答案

这里是答案:

from sqlalchemy.sql.expression import Tuple
session.query(Foo).filter(Tuple(Foo.a, Foo.b).in_([(1,2), (3,4)])).all()

关于postgresql - SQLAlchemy:使用 `IN` 运算符查询复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481552/

相关文章:

python - 将 Django 连接到 Postgres : django. db.utils.OperationalError: FATAL: database "DATABASENAME"does not exist

MYSQL:不能/如何使用子查询作为完全限定的 CTE

python - 一种将 psql 表(或查询)作为文件(csv、json)直接导出到 AWS S3 的方法

mysql - SQLAlchemy - 删除孙子

python - 导入错误 : cannot import name 'PY2'

mysql - python : sqlalchemy - how do I ensure connection not stale using new event system

r - 如何使用多列作为键合并两个数据框?

ruby-on-rails - Rails Enum 返回整数值,而不是字符串表示

SQL复合键

java - JPA 中的复合键