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 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-04-09 06:31:17 +00:00
parent 034b858fc9
commit 0f33feb6d6

View File

@@ -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
""")