SQLAlchemy在没有显式声明回滚调用的情况下,会话的未提交更改是否会自动回滚?

2024-05-15 00:20:15 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,此代码:

db = SQLAlchemy()

def myfunction(a):
   #somechanges in database
   if a == 2:
     return
   db.session.commit()

myfunction(2) # there were some changes here that were not committed neither rolled back
myfunction(4) # Here the changes were committed.

我的问题是,在第二次调用中,第一次更改是否与第二次更改一起提交?在

提前谢谢


Tags: 代码indbreturnifsqlalchemysessiondef
2条回答

如果您阅读the tutorial,您将看到会话具有dirty属性:

db = SQLAlchemy()

def myfunction(a):
   #somechanges in database
   if a == 2:
     return
   db.session.commit()

myfunction(2) # there were some changes here that were not committed neither rolled back
print(db.session.dirty)
myfunction(4) # Here the changes were committed.

它告诉你什么?在

myFunction(4)调用中执行的更改将覆盖对myFunction(2)调用中执行的更改。无论是否提交,更新数据都是如此。在

对于添加行和列,没有数据重写。在函数调用myFunction(4)之前提交不会产生任何影响。在

相关问题 更多 >

    热门问题