Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6085377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:37:55+00:00 2026-05-23T11:37:55+00:00

I don’t get it, but this code doesn’t call after_flush/before_flush/after_flush_postexec # -*- coding: utf-8

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:37:56+00:00Added an answer on May 23, 2026 at 11:37 am

    Michael Bayer answered on SQLAlchemy’s mailing list 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.

    Thanks, Michael

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't understand why this code doesn't work: function Messages(type,text) { console.log(In function Message);
I don't understand where the extra bits are coming from in this article about
Don't need to do this right now but thinking about the future... What would
Don't know if this is the right place to ask this, but I will
Don't know if this is an eclipse specific problem but whenever I declare a
Don't ask me how but I managed to get accidentally the following remote branches
Don't get me wrong, I want them to get saved. But I always thought
I don't know if this has been asked before, but what i'd like to
Don't get me wrong - I love Smalltalk, but... To me, the Squeak interface
Don't ask me why but i can't use this method because I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.