From 1d83a339b6008b1743846ea5ab32fe15abe644a9 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 21 Feb 2026 22:43:20 +0000 Subject: [PATCH] Upsert followers in add_follower to prevent IntegrityError Co-Authored-By: Claude Opus 4.6 --- services/federation_impl.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/services/federation_impl.py b/services/federation_impl.py index 9ae761a..6574dbe 100644 --- a/services/federation_impl.py +++ b/services/federation_impl.py @@ -286,6 +286,23 @@ class SqlFederationService: if actor is None: raise ValueError(f"Actor not found: {username}") + # Upsert: update if already following, insert if new + existing = ( + await session.execute( + select(APFollower).where( + APFollower.actor_profile_id == actor.id, + APFollower.follower_acct == follower_acct, + ) + ) + ).scalar_one_or_none() + + if existing: + existing.follower_inbox = follower_inbox + existing.follower_actor_url = follower_actor_url + existing.follower_public_key = follower_public_key + await session.flush() + return _follower_to_dto(existing) + follower = APFollower( actor_profile_id=actor.id, follower_acct=follower_acct,