2017年12月6日 星期三

[ Python 常見問題 ] Sqlalchemy in clause

Source From Here 
Question 
I'm trying to do this query in SQLAlchemy
SELECT id, name FROM user WHERE id in (123,456)

I would like to bind the list [123,456] at execution time. 

How-To 
How about session.query(MyUserClass).filter(MyUserClass.id.in_((123,456))).all() ? 
  1. session.execute(  
  2.     select(  
  3.         [MyUserTable.c.id, MyUserTable.c.name],   
  4.         MyUserTable.c.id.in_((123456))  
  5.     )  
  6. ).fetchall()  
select() takes two parameters, the first one is a list of fields to retrieve, the second one is the where condition. You can access all fields on a table object via the c (or columns) property.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...