SQLAlchemy 0.6.5 (and 0.6.8) SessionExtension after_flush doesn't gets called

56 views
Skip to first unread message

kost BebiX

unread,
Jun 25, 2011, 4:33:38 AM6/25/11
to sqlal...@googlegroups.com
I've created this question at stackoverflow http://stackoverflow.com/questions/6476652/sqlalchemy-0-6-5-and-0-6-8-sessionextension-after-flush-doesnt-gets-called , but I thought maybe I should also ask here.

I don't get it, but this code doesn't call 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()

And a result:

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

Thank you.

Michael Bayer

unread,
Jun 26, 2011, 12:54:34 PM6/26/11
to sqlal...@googlegroups.com
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.






Thank you.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/40uHGUoHJ1sJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

kost BebiX

unread,
Jun 28, 2011, 4:55:29 PM6/28/11
to sqlal...@googlegroups.com
Oh, thank you very much, that makes sense to me (and I knew I am wrong somewhere because there are even tests for those after_flush events that work).
Reply all
Reply to author
Forward
0 new messages