batchRequestIdx is undefined for a failed query in $transaction([...]) on driver adapters
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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from prisma/orm
-
bug/1-unconfirmed kind/bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
get-platform: distros with ID_LIKE=alpine are not detected as musl, so glibc engines are used on Alpine-based imagesPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
prisma/orm#31281 · 1 comment ·
Maintainers usually reply within 1 day
-
adapter-pg: connectToShadowDb connects to the main database when built from a connection stringMay be free again @cestercian claimed this 10 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
prisma/orm#31196 · 1 comment ·
Maintainers usually reply within 1 day
-
[Prisma 8] Type-check time grows exponentially with chained .include() callsPossibly taken @SiKreuz claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
prisma/orm#31161 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
prisma/orm#31125 · 4 comments · 6 reactions ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tenstorrent/ttnn-visualizer#2526 ·
Maintainers usually reply within 1 day
-
trySessionCache calls /get-session twice when minting session_data (cache miss or expired cookie)Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
neondatabase/neon-js#301 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tashfeenahmed/freellmapi#2305 ·
Maintainers usually reply within 1 day