Symptom: an ownership-transfer endpoint returned the OLD owner's username after setting obj.owner_id = new_id; session.flush() and serializing via obj.owner.username. Deterministic in a test harness that shares one Session across app requests (identity map keeps the instance alive with the owner relationship already loaded from a prior request); appears flaky because it depends on whether a commit expired the instance in between. Changing an FK attribute does NOT refresh an already-loaded relationship. Fix: assign the relationship object itself (obj.owner = new_owner) — SQLAlchemy syncs the FK and the relationship can't go stale. Debugging tip: 'owner' in obj.__dict__ tells you whether the relationship is loaded (and thus can be stale) at any point.
lesson
SQLAlchemy: update the relationship, not the FK, when the instance may be identity-map cached
No signals yet