Sqlalchemy session.refresh does not refresh object

后端 未结 4 758
[愿得一人]
[愿得一人] 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条回答
  •  情话喂你
    2021-02-05 17:53

    From the docs:

    Note that a highly isolated transaction will return the same values as were previously read in that same transaction, regardless of changes in database state outside of that transaction

    SQLAlchemy uses a transactional unit of work model, wherein each transaction is assumed to be internally consistent. A session is an interface on top of a transaction. Since a transaction is assumed to be internally consistent, SQLAlchemy will only (well, not quite, but for ease of explanation...) retrieve a given piece of data from the database and update the state of the associated objects once per transaction. Since you already queried for the object in the same session transaction, SQLAlchemy will not update the data in that object from the database again within that transaction scope. If you want to poll the database, you'll need to do it with a fresh transaction each time.

提交回复
热议问题