From ac7892dd3234a8983fe9838d6f8bb3b77ffed86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=BChn?= Date: Wed, 16 Sep 2026 11:04:16 +0200 Subject: [PATCH] =?UTF-8?q?Bugfix:=20lastInsertRowid=20in=20PostgreSQL-Tra?= =?UTF-8?q?nsaktionen=20(txDb=20haengt=20RETURNING=20id=20an)=20=E2=80=94?= =?UTF-8?q?=20behob=20500=20beim=20Abschicken=20von=20Vorlagen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/db.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/backend/db.js b/backend/db.js index 8e2ddbd..2118d88 100644 --- a/backend/db.js +++ b/backend/db.js @@ -85,10 +85,21 @@ if (usePostgres) { prepare(sql) { const pgSql = convertPlaceholders(sql); return { - run: (...params) => client.query(pgSql, params).then(result => ({ - changes: result.rowCount, - lastInsertRowid: result.rows[0]?.id || null, - })), + run: (...params) => { + // BUGFIX (v8): lastInsertRowid innerhalb der Transaktion — + // identische RETURNING-Logik wie im outer prepare(). Ohne + // RETURNING id liefert pg keine rows → lastInsertRowid war + // null → Folge-Inserts (task_values mit task_id=null) schlugen + // mit FK-Verstoß fehl (500 beim Abschicken einer Vorlage). + const isInsert = pgSql.trim().toUpperCase().startsWith('INSERT'); + const finalSql = isInsert && !pgSql.toUpperCase().includes('RETURNING') + ? pgSql.replace(/;?\s*$/, ' RETURNING id') + : pgSql; + return client.query(finalSql, params).then(result => ({ + changes: result.rowCount, + lastInsertRowid: result.rows[0]?.id || null, + })); + }, get: (...params) => client.query(pgSql, params).then(result => result.rows[0] || null), all: (...params) => client.query(pgSql, params).then(result => result.rows), };