LoginSignup
3
3

More than 5 years have passed since last update.

SQLAlchemyでtupleを含むwhere句を使用する(バックエンド依存)

Posted at

MySQLでは以下のようにtupleを使ってwhere句を書くことができる。

select
  *
from table_schema.table t
where (t.col1, t.col2) in (
(val1, val2),
(val3, val4),
)
;

これをSqlAlchemyで記述する場合は、以下のようになる。

from sqlalchemy import tuple_

query = session.query(table).filter(
    tuple_(table.c.col1, table.c.col2).in_(
        [(val1, val2), (val3, val4), ]
    )
)
# 当然 = も可能
query = session.query(table).filter(
    tuple_(table.c.col1, table.c.col2)
    == (val1, val2)
)

参考URL
http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html?highlight=tuple#sqlalchemy.sql.expression.tuple_

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3