SQLAlchemy没有明确说明回滚调用,会话的未更改会自动回滚吗?

时间:2016-07-26 13:48:50

标签: python sqlalchemy

例如这段代码:

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.

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

提前致谢

2 个答案:

答案 0 :(得分:1)

myFunction(4)的调用中执行的更改将覆盖对myFunction(2)的调用中执行的更改。这是更新数据,提交与否的情况。

为了添加行和列,不会覆盖数据。在函数调用myFunction(4)之前提交将不会产生任何影响。

答案 1 :(得分:-1)

如果您阅读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.

它告诉你什么?