From 0f33feb6d6296f972ee67f9164eb6fe5dea55164 Mon Sep 17 00:00:00 2001 From: chihlasm Date: Thu, 9 Apr 2026 06:31:17 +0000 Subject: [PATCH] fix: use correlated subquery in psa_post_log backfill to avoid invalid FROM-clause reference PostgreSQL UPDATE...FROM does not allow the updated table to be referenced inside the FROM clause's JOIN conditions. Replace the LEFT JOIN psa_connections with a correlated subquery. Co-Authored-By: Claude Opus 4.6 --- .../8aac5b372402_add_account_id_psa_notifications.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 """)