gpt4 book ai didi

python - 当 session.flush() 在 SQLAlchemy 上失败时,我应该调用回滚吗?

转载 作者:可可西里 更新时间:2023-11-01 07:07:00 24 4
gpt4 key购买 nike

我知道当 session.commit() 失败时调用回滚,例如 try-except block 。
但是当session.flush()失败时,是否应该执行rollback()呢?

try:
session.flush()
except IntegrityError:
session.rollback()

最佳答案

接受的答案并不完全正确。关于 Session.flush() 的文档对此有点误导。

失败时,事务(即数据库事务)将回滚(由数据库)。它的 python 对应物 Session 对象本身将处于“非事件”状态,并且根据文档 1 2

must be explicitly rolled back by the calling application, in the same way that it would otherwise need to be explicitly committed if a failure had not occurred.

它进一步承认

this is a common error when using the ORM

关于python - 当 session.flush() 在 SQLAlchemy 上失败时,我应该调用回滚吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305254/

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