Hi, I had a question regarding transactions. I am ...
# orm-help
a
Hi, I had a question regarding transactions. I am observing the following:
Copy code
// WORKS - (rollback of write2 happens if write1 fails)
const write1 = prisma.user.create()
const write2 = prisma.post.create()
await prisma.$transaction([write1, write2])


// BROKEN? - (write2 is NOT rolled back if write1 fails)
const write1 = prisma.user.create()
const write2 = prisma.post.create()
write2.then(() => console.log('something happened'))
await prisma.$transaction([write1, write2])
Is this behavior expected?
d
Hey @ali đź‘‹, That seems correct. By calling
write2.then(() => console.log('something happened'))
you are initiating the query and excluding it from the transaction you’re creating in the next line.
j
Hey @Daniel Norman - Thanks for answering so quick! Would you be able to double check this though? It would be quite a strange deviation from the native `Promise` behavior to have
.then
trigger a side effect of executing the transaction. If that is indeed the expected behavior, could you point out the source code or docs about it? I know that the return value of prisma executions is not a “normal” promise (looks to be
PrismaPromise<A> = Promise<A> & {[prisma]: true}
), but the semantics of
.then
are to invoke a callback once the task has been completed, not to trigger it to happen.
So I guess I have a feature request then to expose something like
.promise
which lets you tap into the execution without triggering it.
r
@James Fox 👋 You can also now use long running transactions in this case 🙂 https://github.com/prisma/prisma/releases/tag/2.29.0