Skip to content

Commit b722b75

Browse files
authored
Merge pull request #256 from Paca-AI/hotfix/fix-error-cannot-run-migration-000018
fix: nullify orphaned references in triggered_by_member_id before add…
2 parents 662fde5 + 5fc216a commit b722b75

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

services/api/migrations/000018_add_automation_workflows.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ CREATE INDEX IF NOT EXISTS idx_workflow_edges_target ON workflow_edges (target_n
106106
ALTER TABLE agent_conversations
107107
ALTER COLUMN triggered_by_member_id DROP NOT NULL;
108108

109+
-- The triggered_by_member_id column was created in 000008 without a FK
110+
-- constraint, so production databases may contain rows whose
111+
-- triggered_by_member_id no longer references an existing project_members row
112+
-- (e.g. the member was hard-deleted or the membership was removed). Adding
113+
-- the FK constraint below would fail on those rows with SQLSTATE 23503.
114+
-- Nullify orphaned references before adding the constraint; this is safe now
115+
-- that the column is nullable.
116+
UPDATE agent_conversations ac
117+
SET triggered_by_member_id = NULL,
118+
updated_at = NOW()
119+
WHERE triggered_by_member_id IS NOT NULL
120+
AND NOT EXISTS (
121+
SELECT 1 FROM project_members pm WHERE pm.id = ac.triggered_by_member_id
122+
);
123+
109124
DO $$
110125
BEGIN
111126
IF NOT EXISTS (

0 commit comments

Comments
 (0)