diff --git a/backend/alembic/versions/8aac5b372402_add_account_id_psa_notifications.py b/backend/alembic/versions/8aac5b372402_add_account_id_psa_notifications.py index 9b39fa04..1637e0b1 100644 --- a/backend/alembic/versions/8aac5b372402_add_account_id_psa_notifications.py +++ b/backend/alembic/versions/8aac5b372402_add_account_id_psa_notifications.py @@ -25,11 +25,15 @@ def upgrade() -> None: # Step 2: BACKFILL # psa_post_log: prefer psa_connection → fallback to posted_by user + # Note: cannot reference the updated table (ppl) inside the FROM clause JOIN, + # so use a correlated subquery for psa_connections lookup instead. op.execute(""" UPDATE psa_post_log ppl - SET account_id = COALESCE(pc.account_id, u.account_id) + SET account_id = COALESCE( + (SELECT account_id FROM psa_connections WHERE id = ppl.psa_connection_id), + u.account_id + ) FROM users u - LEFT JOIN psa_connections pc ON pc.id = ppl.psa_connection_id WHERE ppl.posted_by = u.id AND ppl.account_id IS NULL """)