Bugfix: lastInsertRowid in PostgreSQL-Transaktionen (txDb haengt RETURNING id an) — behob 500 beim Abschicken von Vorlagen

This commit is contained in:
Kühn
2026-09-16 11:04:16 +02:00
parent 1ee37c402a
commit ac7892dd32

View File

@@ -85,10 +85,21 @@ if (usePostgres) {
prepare(sql) { prepare(sql) {
const pgSql = convertPlaceholders(sql); const pgSql = convertPlaceholders(sql);
return { return {
run: (...params) => client.query(pgSql, params).then(result => ({ 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, changes: result.rowCount,
lastInsertRowid: result.rows[0]?.id || null, lastInsertRowid: result.rows[0]?.id || null,
})), }));
},
get: (...params) => client.query(pgSql, params).then(result => result.rows[0] || null), get: (...params) => client.query(pgSql, params).then(result => result.rows[0] || null),
all: (...params) => client.query(pgSql, params).then(result => result.rows), all: (...params) => client.query(pgSql, params).then(result => result.rows),
}; };