Sqlalchemy session.refresh does not refresh object

后端 未结 4 753
[愿得一人]
[愿得一人] 2021-02-05 17:27

I have the following mapping (straight from SA examples):

class User(Base):
    __tablename__ = \'users\'

    id = Column(Integer, primary_key=True)
    name =          


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 17:59

    session.refresh() didn't work for me either. Even though I saw a low-level SELECT the object was not updated after the refresh.

    This answer https://stackoverflow.com/a/11121788/562267 hints to doing an actual commit/rollback to reset the session, and that worked for me:

    user_1 = session.query(User).filter(User.id==1).one()
    user_1.name # This prints: u'user1'
    # update the database from another client here
    session.commit()
    user_1 = session.query(User).filter(User.id==1).one()
    user_1.name # Should be updated now.
    

提交回复
热议问题