gpt4 book ai didi

python - sqlalchemy:为什么我不能更新到 func.now(),但可以使用 'now()'?

转载 作者:太空狗 更新时间:2023-10-29 20:20:27 38 4
gpt4 key购买 nike

...当我尝试执行如下所示的查询时:

Session().query(MyMappedClass).update({MyMappedClass.time:func.now()})

我得到:

InvalidRequestError: Could not evaluate current criteria in Python. Specify 'fetch' or False for the synchronize_session parameter.

但如果我这样做:

Session().query(MyMappedClass).update({MyMappedClass.time:'now()'})

...它有效。有人知道为什么吗?

最佳答案

这在 documentation for update() 中有解释

synchronize_session –

chooses the strategy to update the attributes on objects in the session. Valid values are:

False - don’t synchronize the session. This option is the most efficient and is reliable once the session is expired, which typically occurs after a commit(), or explicitly using expire_all(). Before the expiration, updated objects may still remain in the session with stale values on their attributes, which can lead to confusing results.

'fetch' - performs a select query before the update to find objects that are matched by the update query. The updated attributes are expired on matched objects.

'evaluate' - Evaluate the Query’s criteria in Python straight on the objects in the session. If evaluation of the criteria isn’t implemented, an exception is raised.

update() 默认情况下想要刷新缓存在 Session 中的那些对象而不进行数据库往返。 func.now() 是一个 SQL 函数,需要进行此往返。发送字符串 “now()” 不是您想要的,因为这会将字段的值设置为字符串“now()”,并且不会实际使用 SQL 时间函数。您应该将 synchronize_session=False 传递给 update()。

关于python - sqlalchemy:为什么我不能更新到 func.now(),但可以使用 'now()'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13406480/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com