sqlalchemy -> expire_on_commit

发布时间 2023-11-18 11:58:02作者: bitterteaer

当 expire_on_commit=True 时,commit 之后所有实例都会过期,之后再访问这些过期实例的属性时,SQLAlchemy 会重新去数据库加载实例对应的数据记录。

# SQLAlchemy 源码,非关键内容省略
class Session(_SessionClassMethods):
    """
    Manages persistence operations for ORM-mapped objects.
    The Session's usage paradigm is described at :doc:`/orm/session`.
    """
    ...
    def __init__(
        self,
        bind=None,
        autoflush=True,
        expire_on_commit=True,
        ...
    ):
    """
        :param expire_on_commit:  Defaults to ``True``. When ``True``, all
           instances will be fully expired after each :meth:`~.commit`,
           so that all attribute/object access subsequent to a completed
           transaction will load from the most recent database state.
    """

原文链接: https://youguanxinqing.xyz/archives/147/