I have working listens_for function, which inserts LoadHistory
object before updating Load
:
@event.listens_for(Load, 'before_update')
def before_update_load(mapper, connection, target):
current_load_select = select(load_fields).where(Load.id == target.id)
insert_history = insert(LoadHistory).from_select(load_history_fields, current_load_select)
connection.execute(insert_history)
And the task is to have similar function but which will insert LoadStopHistory
objects after LoadHistory
object has been inserted. The code is:
@event.listens_for(LoadHistory, 'after_insert')
def after_insert_load_history(mapper, connection, target):
# here will be code
print("@" * 125)
print('trgt', target)
print("@" * 125)
but the issue that this function has never been called.
I’ve tried to insert LoadHistory
with updating Load
or directly with Postgres, neither practice had any result.
I’ve tried with ‘after_insert’ changed to ‘before_insert’ and ‘before_update’.
2
Answers
There are various possible causes to look into if your event listener for the
after_insert
event on theLoadHistory
model is not being called. The relationships between your SQLAlchemy models (Load
,LoadHistory
, andLoadStopHistory
) should be properly specified first because events may not occur in the absence of such relationships. Second, keep in mind that events are frequently started during the SQLAlchemy session commit, so be sure to do so after entering a "LoadHistory" object. Make that your event listeners are registered correctly and are executed when your application launches. Be mindful of database transactions; if one is reversed before the session commits, it may prevent events from happening.Hope this helps, do let me know if you want help
The impression I get from this note (from Mapper-level Flush Events):
is that the after_insert event will not register the result of calling
insert()
directly – the do_orm_execute listener must be used instead. Moreover, the insert should be executed using the session rather than the connection to be registered by the ORM event machinery.These listeners seem to work: