batchRequestIdx is undefined for a failed query in $transaction([...]) on driver adapters

Open
#31,238 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, sqlite, typescript
Domain
database

Research direction

Start with the 'multi' branch of ClientEngine.requestBatch, then compare RemoteExecutor and LocalExecutor error handling to see where batchIndex is preserved. Reproduce with the provided transaction and run the tests beside "correctly reports location of a batch error" in tests/functional/issues/14373-batch-tx-error. Done means failed driver-adapter and compacted-query errors report the correct zero-based batchRequestIdx, with regression coverage; note that a fix is already described in a linked PR.

Written by the indexing model from the issue text.

Description

Written by an AI agent (Amp) on behalf of @jasonkuhrt.

Package and version

@prisma/client 7.10.1 with @prisma/adapter-better-sqlite3 7.10.1. Also reproduces on 7.11.0-dev.4, the latest build of the v7 branch.

What happened?

When a query in $transaction([...]) fails on a driver adapter, the error's batchRequestIdx is undefined, so a caller can't tell which query of the batch failed.

batchRequestIdx is a public field of PrismaClientKnownRequestError and PrismaClientUnknownRequestError. #16240 added it so that the client could "identify and correctly report error location within a batch", and waitForBatch still uses it to pick the error a batch rejects with. The other ways of running the same batch set it:

Client batchRequestIdx of the failed query
6.19.3, Rust query engine 1
7.10.1, RemoteExecutor (Accelerate) set from batchIndex (RemoteExecutor.ts)
7.10.1, driver adapter (LocalExecutor) undefined

The cause is the 'multi' branch of ClientEngine.requestBatch on v7: it passes batchIndex to executor.execute, and its catch pushes the error into the results as it was thrown. RemoteExecutor puts that index on its error, while the local executor's errors carry none.

What did you expect to happen?

error.batchRequestIdx is the failed query's position in the batch (1 below), as in Prisma 6 and through RemoteExecutor.

Minimal reproduction

package.json:

{ "type": "module", "private": true }

schema.prisma:

generator client {
  provider = "prisma-client"
  output   = "./generated"
}

datasource db {
  provider = "sqlite"
}

model User {
  id    Int    @id @default(autoincrement())
  email String @unique
}

prisma.config.ts:

import { defineConfig } from 'prisma/config'

export default defineConfig({
  schema: 'schema.prisma',
  datasource: { url: 'file:./dev.db' },
})

repro.ts:

import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { PrismaClient } from './generated/client'

const prisma = new PrismaClient({ adapter: new PrismaBetterSqlite3({ url: 'file:./dev.db' }) })

await prisma.user.deleteMany()
await prisma.user.create({ data: { email: '[email protected]' } })

try {
  await prisma.$transaction([
    prisma.user.findMany(),
    prisma.user.create({ data: { email: '[email protected]' } }), // fails: unique constraint
    prisma.user.findMany(),
  ])
} catch (error: any) {
  console.log('$transaction([...]):', { code: error.code, batchRequestIdx: error.batchRequestIdx })
}

await prisma.$disconnect()
npm i [email protected] @prisma/[email protected] @prisma/[email protected] tsx@4
npx prisma db push
npx prisma generate
npx tsx repro.ts
$transaction([...]): { code: 'P2002', batchRequestIdx: undefined }
The same batch on Prisma 6

In a new directory, the same package.json, no prisma.config.ts, and:

schema.prisma:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model User {
  id    Int    @id @default(autoincrement())
  email String @unique
}

repro.ts: the script above with its two imports and its const prisma line replaced by

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
npm i [email protected] @prisma/[email protected] tsx@4
npx prisma db push
npx tsx repro.ts
$transaction([...]): { code: 'P2002', batchRequestIdx: 1 }
Environment
  • Node: 26.13.1
  • OS: Linux x64
  • Package manager: npm
  • Database: SQLite (@prisma/adapter-better-sqlite3)
Additional context

A batch of findUniqueOrThrow calls that Prisma runs as one compacted query also rejects with no index when one record is not found, though convertCompactedRows knows its position. Prisma 6 left that index out too.

A fix for both, with tests beside "correctly reports location of a batch error" in tests/functional/issues/14373-batch-tx-error, is in a PR that links this issue.

Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.