@@ -525,8 +525,12 @@ def log_change(self, session, item_id, operation, changes, table_name, operation
525525 # Combine all changes into a single string with their types
526526 changes_string = '\r \n \r \n ' .join (f"{ column_name } : [{ type (old_value ).__name__ } ] { old_value } ----> [{ type (new_value ).__name__ } ] { new_value } " for column_name , old_value , new_value in changes )
527527
528+ # Do not use the nasty "or" expression, item_id may be 0
529+ if item_id is None :
530+ item_id = generated_id
531+
528532 change = OPERATION_LOG_BASE (
529- item_id = item_id or generated_id ,
533+ item_id = item_id ,
530534 operation_id = operation_id ,
531535 operation = operation ,
532536 last_modified = datetime .datetime .now (tz = timezone .utc ),
@@ -567,9 +571,6 @@ def log_changes_before_commit(self, session):
567571 if isinstance (obj , OPERATION_LOG_BASE ):
568572 continue # Skip change log entries
569573
570- item_id = getattr (obj , list (obj .__table__ .primary_key .columns .keys ())[0 ])
571- generated_id = None
572-
573574 #Avoid logging rollback operations
574575 if operation == 'ROLLBACK' :
575576 return
@@ -578,6 +579,10 @@ def log_changes_before_commit(self, session):
578579 if operation == 'INSERT' :
579580 session .flush ()
580581
582+ # Retrieve the attribute after session flush
583+ item_id = getattr (obj , list (obj .__table__ .primary_key .columns .keys ())[0 ])
584+ generated_id = None
585+
581586 if operation == 'UPDATE' :
582587 changes = []
583588 for attr in class_mapper (obj .__class__ ).column_attrs :
0 commit comments