gpt4 book ai didi

python - SQLAlchemy 0.6.5(和0.6.8)SessionExtension after_flush 不会被调用

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:48 26 4
gpt4 key购买 nike

我不明白,但是这段代码没有调用 after_flush/before_flush/after_flush_postexec

# -*- coding: utf-8 -*-

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm.interfaces import SessionExtension

class AfterFlushExtension(SessionExtension):
def before_commit(self, session):
print "> before_commit"

def after_commit(self, session):
print "> after_commit"

def before_flush(self, session, flush_context, instances):
print '> before_flush'

def after_flush(self, session, flush_context):
print '> after_flush'

def after_flush_postexec(self, session, flush_context):
print '> after_flush_postexec'

session = scoped_session(sessionmaker(extension=AfterFlushExtension()))
session.flush()
session.commit()

结果:

$ python ~/Dropbox/playground/python/sqlalchemy_hook_test/main.py 
> before_commit
> after_commit

最佳答案

Michael Bayer 在 SQLAlchemy 的邮件列表上回复 https://groups.google.com/d/msg/sqlalchemy/GrMZGtJ-yc8/mCviGB6g9HYJ :

The flush events only fire off if there's actually something to be flushed. It would be inefficient for the events to be emitted for every flush() as flush is in fact called a great number of times, on every query, assuming autoflush enabled. For this reason a flush() with a session that has no change events of any kind quickly checks some flags and returns.

Think of before_flush() really being called before_flush_on_pending_changes() if that helps.

I'll check the docstrings to see if any clarification is needed.

谢谢,迈克尔

关于python - SQLAlchemy 0.6.5(和0.6.8)SessionExtension after_flush 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6476652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com