TransactionRollbackError with BetterSQLite3 #1170
-
When performing a rollback inside a transaction I get an error:
Packages: "drizzle-kit": "^0.19.13",
"drizzle-orm": "^0.28.5",
"better-sqlite3": "^8.5.2",
"next": "13.4.19", Code snippet: // Start transaction
const res = await db.transaction(async (tx) => {
// Insert workout
const [{ id: workoutId }] = await tx
.insert(workouts)
.values({
date: workout.date,
userId,
}).returning()
await tx.rollback()
return
} Expected results:
Actual results:
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
Hey I also faced the same issue, did you find any solution ? |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing the same issue with Postgres - any luck finding a solution? |
Beta Was this translation helpful? Give feedback.
-
Facing this issue too with Postgres |
Beta Was this translation helpful? Give feedback.
-
You don't need to await the rollback. Can you check if your code works without awaiting the transaction? If your code still inserts the workout after rolling back, can you put together a reproduction repo to investigate? |
Beta Was this translation helpful? Give feedback.
-
Hey @Angelelz, I faced the same issue and following your reply I tried it myself, but after some time testing things I still am unable to rollback the transaction before the update and the delete. Do you know how I could fix that? Or am I missing a point? function modifyExpense({ expenseID, input }) {
return db.transaction((tx) => {
const [expense] = tx
.update(expenses)
.set({
categoryId: input.categoryID,
description: input.description,
groupId: input.groupID,
date: validISODate(input.date),
})
.where(eq(expenses.id, expenseID))
.returning()
.all();
if (input.dues.length) {
tx.delete(expense_dues)
.where(eq(expense_dues.expenseId, expenseID))
.run();
// rolling back for testing purposes
tx.rollback();
tx.insert(expense_dues).values(input.dues).run();
}
return expense;
});
}, |
Beta Was this translation helpful? Give feedback.
I think that the issue is that BetterSQLite3 is a synchronous sqlite library.
You don't need to await anything, just use the all, run, get and values methods available in all the query builders.
Please see the changes I made to your index.ts file: