
Batched statements are SQL transactions ↗. If a statement in the sequence fails, then an error is returned for that specific statement, and it aborts or rolls back the entire sequence.Getting a transaction error when using Drizzle ORM's .transaction() method with Cloudflare D1 database.Error: To execute a transaction, please use the state.storage.transaction() or state.storage.transactionSync() APIs instead of the SQL BEGIN TRANSACTION or SAVEPOINT statements. The JavaScript API is safer because it will automatically roll back on exceptions, and because it interacts correctly with Durable Objects' automatic atomic write coalescing.// This code fails in D1
const result = await this.db.transaction(async (tx) => {
const updatedUser = await tx.update(user)
.set(userUpdateData)
.where(eq(user.id, input.id))
.returning()
.get();
if (!updatedUser) {
throw new Error("User not found");
}
// Additional operations...
return updatedUser;
});Batched statements are SQL transactions ↗. If a statement in the sequence fails, then an error is returned for that specific statement, and it aborts or rolls back the entire sequence.