Skip to content

Commit 02a589e

Browse files
itigges22claude
andcommitted
fix: nullify workflow_history node references before deleting nodes
workflow_history.from_node_id and to_node_id also reference workflow_nodes, causing FK constraint violations on save. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 01577a2 commit 02a589e

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

  • app/api/admin/workflows/templates/[id]/steps

app/api/admin/workflows/templates/[id]/steps/route.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,32 @@ export async function PUT(
227227
const existingNodeIds = existingNodes?.map(n => n.id) || [];
228228
logger.debug('[Workflow Save] Found existing nodes', { count: existingNodeIds.length });
229229

230-
// Clear FK references to these nodes before deleting them
230+
// Clear ALL FK references to these nodes before deleting them
231231
if (existingNodeIds.length > 0) {
232-
logger.debug('[Workflow Save] Clearing FK references for node deletion');
232+
logger.debug('[Workflow Save] Clearing FK references for node deletion', { count: existingNodeIds.length });
233233

234-
// Nullify current_node_id on workflow_instances
234+
// 1. Nullify current_node_id on workflow_instances
235235
await supabase
236236
.from('workflow_instances')
237237
.update({ current_node_id: null })
238238
.in('current_node_id', existingNodeIds);
239239

240-
// Delete workflow_active_steps referencing these nodes
240+
// 2. Delete workflow_active_steps referencing these nodes
241241
await supabase
242242
.from('workflow_active_steps')
243243
.delete()
244244
.in('node_id', existingNodeIds);
245+
246+
// 3. Nullify workflow_history from_node_id and to_node_id references
247+
await supabase
248+
.from('workflow_history')
249+
.update({ from_node_id: null })
250+
.in('from_node_id', existingNodeIds);
251+
252+
await supabase
253+
.from('workflow_history')
254+
.update({ to_node_id: null })
255+
.in('to_node_id', existingNodeIds);
245256
}
246257

247258
// Delete existing nodes and connections for this template

0 commit comments

Comments
 (0)