@@ -58,20 +58,23 @@ class UserMixin(MappedAsDataclass):
5858
5959``` python
6060@event.listens_for (UserMixin, ' before_insert' , propagate = True )
61- def set_created_by (mapper , connection , target ) -> None : # noqa: ANN001
62- if hasattr (target, ' created_by' ):
61+ def _inject_created_by (mapper , connection , target ) -> None : # noqa: ANN001
62+ user_id = ctx.user_id
63+ if user_id is None :
64+ return
65+ if hasattr (target, ' created_by' ) and target.created_by is None :
6366 target.created_by = ctx.user_id
6467
6568
6669@event.listens_for (Session, ' do_orm_execute' , propagate = True )
67- def set_updated_by ( orm_statement : ORMExecuteState) -> None :
70+ def _inject_updated_by ( orm_execute_state : ORMExecuteState) -> None :
6871 if (
69- orm_statement .is_update
70- and orm_statement .is_orm_statement
71- and orm_statement .statement.is_update
72- and orm_statement .bind_mapper.c.get(' updated_by' ) is not None
72+ orm_execute_state .is_update
73+ and orm_execute_state .is_orm_statement
74+ and orm_execute_state .statement.is_update
75+ and orm_execute_state .bind_mapper.c.get(' updated_by' ) is not None
7376 ):
74- orm_statement .statement = orm_statement .statement.values(updated_by = ctx.user_id)
77+ orm_execute_state .statement = orm_execute_state .statement.values(updated_by = ctx.user_id)
7578```
7679
7780::: warning
0 commit comments