Bugfix: lastInsertRowid in PostgreSQL-Transaktionen (txDb haengt RETURNING id an) — behob 500 beim Abschicken von Vorlagen
This commit is contained in:
@@ -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) => {
|
||||||
changes: result.rowCount,
|
// BUGFIX (v8): lastInsertRowid innerhalb der Transaktion —
|
||||||
lastInsertRowid: result.rows[0]?.id || null,
|
// 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),
|
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),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user