您如何从刚刚scoped_session.commit()编辑的同一张表中读取值

时间:2019-11-12 21:01:21

标签: python flask sqlalchemy flask-sqlalchemy

以下内容似乎无效。我正在尝试向表提交更新,然后立即读取该值,但是该表似乎没有及时更新...尽管它是提交的,因为在此之后我检查表并将该值从20更改为25 。为什么flask SQL Alchemy scoped_session.commit()不能及时更新数据库/我应该如何立即读取更新后的值?

def myFunc(db):
    scoped_session = db.create_scoped_session()
    before = get_detail(1)
    print(before.amount) # is 20 right now
    data = {"id":1, "amount":25}
    save = update_detail(data, scoped_session)
    after = test_get_invest_detail(1)
    print(after.amount) # want it to be 25 but get 20 bc I believe db isn't updated in time

def update_detail(data, scoped_session):
    row = scoped_session.query(MyTable).filter(MyTable.id == data['id'])
    if row:
       row.update(data, synchronize_session=False)
       return 1
    else:
       return 0

def get_detail(id):
    return MyTable.query.filter_by(id=id).first()

0 个答案:

没有答案