From 7f1df8c508b1ed702ab4021c72679a647e26457d Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 22 Feb 2026 09:53:04 -0600 Subject: [PATCH] Fix user audit trail logging stale agency value for old and new Expire the SQLAlchemy cached user object after flush() so the subsequent get_by_id() reloads the agency relationship with fresh data. Previously the identity map returned the same Python object with the old .agency, causing audit logs to record identical old and new values on agency changes. Co-Authored-By: Claude Opus 4.6 --- backend/app/repositories/user_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/repositories/user_repository.py b/backend/app/repositories/user_repository.py index 2e79756..e876450 100755 --- a/backend/app/repositories/user_repository.py +++ b/backend/app/repositories/user_repository.py @@ -116,7 +116,7 @@ class UserRepository: if agency_id is not ...: user.agency_id = agency_id await self.session.flush() - # Re-fetch to get agency relationship loaded + self.session.expire(user) # Clear cached state so relationship reloads return await self.get_by_id(user_id) async def list_agencies(self) -> list[Agency]: