在 sqlalchemy 的批量更新中的 where 子句中添加多个条件

Adding multiple conditions in where clause in bulk update in sqlalchemy

下面的批量更新代码有效,其中 benefits_dict 是我的词典列表。

conn.execute(MedicalPlanBenefit.__table__.update()
                             .where(MedicalPlanBenefit.__table__.c.user_id == bindparam('user_id')),
                             benefits_dict)

现在,当我像下面这样向我的 where 子句添加多个条件时,它不起作用。

conn.execute(MedicalPlanBenefit.__table__.update()
                             .where(MedicalPlanBenefit.__table__.c.user_id == bindparam('user_id') & MedicalPlanBenefit.__table__.c.test_id == bindparam('test_id')),
                             benefits_dict)

这种情况下如何添加多个条件?

我的benefits_dict

{'user_id': 1, 'email_address' : 'jack@yahoo.com', 'id':12, 'test_id': 31},
   {'user_id': 1, 'email_address' : 'jack@msn.com', 'id':13, 'test_id': 31},
   {'user_id': 2, 'email_address' : 'www@www.org', 'id':14, 'test_id': 31},
   {'user_id': 2, 'email_address' : 'wendy@aol.com', 'id':15, 'test_id': 31} 

您可以将 where 子句链接在一起或使用 and_ 运算符将多个条件添加到您的 where 子句中(请务必从 [=14= 导入 and_ 运算符]).参见 Conjunctions in the SQLAlchemy Expression Language Tutorial。例如:

# Using and_ operator
where(
    and_(
        table.c.id == 'id',
        table.c.name == 'name'
        )
    )

# chaining where clauses
where(table.c.id == 'id').\
where(table.c.name == 'name')