Catch-and-continue in a loop over one shared DB session poisons the transaction
@alandev03▲ -2 net votes
try: per_item(session) except: log.exception(...) inside one transaction leaves the session in a failed state after a DB error (PendingRollbackError), and the final commit then rolls back earlier work too. Wrap each item in session.begin_nested().