question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interactive Transaction not allowed by typescript

See original GitHub issue

Bug description

The following (simple) usage of the interactive transaction (as documented here) produces a nasty typescript error that I can not seem to get around. (My actual transaction contains more logic but I was able to reproduce the error this way.)

const p: Paragraph = await prisma.$transaction(async (prisma) => {
  return prisma.paragraph.create({ data: data });
});
Argument of type '(prisma: any) => Promise<any>' is not assignable to parameter of type 'PrismaPromise<any>[]'.
  Type '(prisma: any) => Promise<any>' is missing the following properties from type 'PrismaPromise<any>[]': pop, push, concat, join, and 27 more.ts(2345)

It seems to expect the simpler $transaction API:

const [posts, totalPosts] = await prisma.$transaction([
  prisma.post.findMany({ where: { title: { contains: 'prisma' } } }),
  prisma.post.count(),
])

How to reproduce

Schema:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialActions"]
}

model Paragraph {
  id      String @id @default(uuid())
  content String
}

Code:

const p: Paragraph = await prisma.$transaction(async (prisma) => {
  return prisma.paragraph.create({ data: data });
});

Expected behavior

Expected to behave as the interactive transaction documentation describes.

Prisma information

See ‘how to reproduce’.

Environment & setup

  • OS: MacOS
  • Database: PostgreSQL
  • Node.js version: v16.4.2
  • Typescript version: 4.3.5

Prisma Version

prisma               : 2.27.0
@prisma/client       : 2.27.0
Current platform     : darwin
Query Engine         : query-engine cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : cdba6ec525e0213cce26f8e4bb23cf556d1479bb
Studio               : 0.410.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pantharshit00commented, Aug 17, 2021
1reaction
ryands17commented, Aug 16, 2021

You don’t have it added to the previewFeatures in your schema which is why it’s not working: https://github.com/prisma/prisma/releases/tag/2.29.0

It should work fine after running prisma generate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma transaction in nodejs and typescript not working
So I use transaction but I got following error -:. Argument of type '(tx: any) => Promise' is not assignable to parameter of...
Read more >
Typing for transaction doesn't include async function argument
Bug description. But typescript doesn't recognize it. Argument of type '(prismaClient: any) => Promise<any>' is not assignable to parameter of ...
Read more >
Transactions and batch queries (Reference) - Prisma
Interactive transactions : pass a function that can contain user code including Prisma Client queries, non-Prisma code and other control flow to be...
Read more >
Cross Module Transaction with Prisma - DEV Community ‍ ‍
Prisma and Interactive Transaction. There's no doubt that Prisma boosts your productivity when dealing with Databases in Node.js + TypeScript.
Read more >
Transactions and batched writes | Firestore - Firebase - Google
Transaction functions should not directly modify application state. Transactions will fail when the client is offline. The following example shows how to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found