SQLAlchemy ORM:在更新中使用func

时间:2017-01-12 13:01:13

标签: python sqlalchemy

我正在尝试在更新语句中使用func,但我收到了InvalidRequest错误(下面的堆栈跟踪)。这是代码:

session.query(C).filter(
    z == True,
).update({
    C.x: func.sqrt((C.y1 + C.y2) * 0.675)
})

这是堆栈跟踪:

  File "/var/xxx/vagrant-env/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3288, in update
    update_op.exec_()
  File "/var/xxx/vagrant-env/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1167, in exec_
    self._do_pre_synchronize()
  File "/var/xxx/vagrant-env/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 1236, in _do_pre_synchronize
    "Could not evaluate current criteria in Python. "
sqlalchemy.exc.InvalidRequestError: Could not evaluate current criteria in Python. Specify 'fetch' or False for the synchronize_session parameter.

我尝试了func.sqrtfunc.round,但他们都给出了上述错误。对我做错了什么的想法?

1 个答案:

答案 0 :(得分:1)

您必须指定synchronize_session strategy - False'fetch'或实施evaluate。只需将其添加为参数,例如

session.query(C).filter(
    z == True,
).update({
    C.x: func.sqrt((C.y1 + C.y2) * 0.675)
}, synchronize_session='fetch')