virtuaboza
virtuaboza
DTDrizzle Team
Created by AstroBear on 1/30/2024 in #help
Do transaction automatically rollback on error?
I came here to ask the same question because it is not working in my case, but my case might be a special case. This is my code:
return db.transaction(async (tx) => {
const organization = (
await tx
.insert(schema.organizations)
.values({
howHeard: input.howHeard,
name: input.organizationName,
})
.returning({ id: schema.organizations.id })
)[0]!;

const library = (
await tx
.insert(schema.libraries)
.values({
defaultConcurrentLoanLimit: input.concurrentLoanLimit,
defaultExtensionLimit: input.extensionLimit,
defaultLoanPeriodDays: input.loanPeriodDays,
excludeHolidaysWeekends: input.excludeHolidaysWeekends,
name: input.libraryName,
organizationId: organization.id,
})
.returning({ id: schema.libraries.id })
)[0]!;

await tx.insert(schema.organizationUsers).values({
organizationId: organization.id,
role: "ADMIN",
userId: ctx.user.id,
});

return library;
});
return db.transaction(async (tx) => {
const organization = (
await tx
.insert(schema.organizations)
.values({
howHeard: input.howHeard,
name: input.organizationName,
})
.returning({ id: schema.organizations.id })
)[0]!;

const library = (
await tx
.insert(schema.libraries)
.values({
defaultConcurrentLoanLimit: input.concurrentLoanLimit,
defaultExtensionLimit: input.extensionLimit,
defaultLoanPeriodDays: input.loanPeriodDays,
excludeHolidaysWeekends: input.excludeHolidaysWeekends,
name: input.libraryName,
organizationId: organization.id,
})
.returning({ id: schema.libraries.id })
)[0]!;

await tx.insert(schema.organizationUsers).values({
organizationId: organization.id,
role: "ADMIN",
userId: ctx.user.id,
});

return library;
});
And this fails for me on the last insert due to https://discord.com/channels/1043890932593987624/1120919676457848946/1120919676457848946, but when I check my database, the new organization and library did get inserted.
5 replies