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:
@@ -25,11 +25,15 @@ def upgrade() -> None:
|
|||||||
|
|
||||||
# Step 2: BACKFILL
|
# Step 2: BACKFILL
|
||||||
# psa_post_log: prefer psa_connection → fallback to posted_by user
|
# 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("""
|
op.execute("""
|
||||||
UPDATE psa_post_log ppl
|
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
|
FROM users u
|
||||||
LEFT JOIN psa_connections pc ON pc.id = ppl.psa_connection_id
|
|
||||||
WHERE ppl.posted_by = u.id
|
WHERE ppl.posted_by = u.id
|
||||||
AND ppl.account_id IS NULL
|
AND ppl.account_id IS NULL
|
||||||
""")
|
""")
|
||||||
|
|||||||
Reference in New Issue
Block a user