Hey Mike,
Thanks for taking the time to answer all of these questions! I think what we're looking for is a way to perform some actions immediately before "COMMIT" is sent to the database. The current solutions (as far as I can understand them) all have some drawbacks:
Perform actions in before_commit
This event fires before a flush event, so the following code performs the actions *before* the data has been flushed:
a = A()
a.name = 'newname'
db.session.commit()
Mark a boolean flag in before_commit, and do actions in after_flush if the boolean is set
This fixes the above problem, but runs into a new issue:
a = A()
a.name = 'newname'
db.session.flush() # Boolean isn't set, so actions don't fire
db.session.commit() # There is no dirty state, so after_flush never gets called here and actions don't fire
It looks like what we're looking for is a "immediately_before_commit_query" hook. Does this exist in SQLAlchemy right now? If not, another alternative we could go with is:
Perform actions in before_commit, but call db.session.flush() first (suggested by Jonathan Vanasco earlier in this thread)
We manually call db.session.flush() to ensure that we don't have any remaining "dirty" state, and in essence we can now perform actions "immediately before the COMMIT query". It would look something like:
@event.listens_for(SignallingSession, 'before_commit')
def actions(...):
db.session.flush()
# Do other actions
I'm not immediately confident that this is equivalent to the conceptual "immediately_before_commit_query" hook - mostly not sure if there are any gotchas with this approach. Do you think this is a safe thing to do?