From cede885b04854fd667f65a19697505c0a7c52739 Mon Sep 17 00:00:00 2001 From: Luca <102960244+Luca-Timo@users.noreply.github.com> Date: Tue, 23 Jun 2026 11:33:14 +0200 Subject: [PATCH] fix(workflows): Postgres-safe id capture on workflow inserts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Postgres, knex .insert() without .returning() resolves to [], so ins[0] was undefined → the child workflow_nodes inserts hit a NOT NULL violation and the whole transaction rolled back. Result on PG: migration + tables present but zero rows — the seeded dunning flow never persisted, and the 'New workflow' button would 500. SQLite returns the row id, so the test harness masked it. Add .returning('id') and normalise the {id} (pg) vs bare-id (sqlite) shapes (same pattern as the crmDb harness) in both the built-in seed and the admin create route. Tests stay green on SQLite (17). --- backend/src/routes/adminWorkflows.js | 7 +++++-- backend/src/services/_workflowSeedBoot.js | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/adminWorkflows.js b/backend/src/routes/adminWorkflows.js index 4392fdba..325af114 100644 --- a/backend/src/routes/adminWorkflows.js +++ b/backend/src/routes/adminWorkflows.js @@ -131,8 +131,11 @@ router.post('/', requirePermission('workflows.manage'), async (req, res, next) = name: b.name, description: b.description || null, enabled: !!b.enabled, version: 1, trigger_type: b.trigger_type, trigger_config: b.trigger_config ? JSON.stringify(b.trigger_config) : null, created_by: req.admin?.id || null, - }); - const newId = ins[0]; + }).returning('id'); + // Postgres returns [] without an explicit returning clause, so ins[0] + // would be undefined → the child node inserts would violate NOT NULL. + // Normalise the {id} (pg) vs bare id (sqlite) shapes. + const newId = ins[0]?.id ?? ins[0]; await writeGraph(trx, newId, 1, b.nodes, b.edges); return newId; }); diff --git a/backend/src/services/_workflowSeedBoot.js b/backend/src/services/_workflowSeedBoot.js index 6601c607..a6a70cc9 100644 --- a/backend/src/services/_workflowSeedBoot.js +++ b/backend/src/services/_workflowSeedBoot.js @@ -127,8 +127,12 @@ async function seedBuiltinWorkflowsAtBoot(db, logger) { trigger_config: JSON.stringify({ seedVersion: SEED_VERSION }), is_builtin: true, builtin_key: DUNNING_KEY, - }); - await writeGraph(trx, ins[0], 1, nodes, edges); + }).returning('id'); + // Postgres returns [] without `.returning`, so ins[0] would be undefined + // and the child node inserts would roll back on NOT NULL. Normalise the + // {id} (pg) vs bare-id (sqlite) shapes. + const workflowId = ins[0]?.id ?? ins[0]; + await writeGraph(trx, workflowId, 1, nodes, edges); }); booted = true;