-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Automatic batching doesn't work inside interactive transaction #25570
Description
Bug description
I've encountered an issue where automatic batching (as described here ) in interactive transactions doesn't work as expected. This is a major issue for me as I'm running a graphQL server and processing each request inside a transaction so I can apply PostgreSQL RLS policies.
If I use the fluent API to query a single relationship, the batching happens as expected, but if I try to query more than one relationship, the batching doesn't happen.
How to reproduce
I'm using a simple schema where there is a User model with two relationships Post and Hat:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id
createdAt DateTime @default(now())
email String @unique
name String?
posts Post[]
hat Hat?
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId String?
}
model Hat {
id Int @id @default(autoincrement())
style String?
user User? @relation(fields: [userId], references: [id])
userId String? @unique
}
If I run the following script, the batching doesn't occur:
const { PrismaClient } = require("@prisma/client");
const client = new PrismaClient();
const run = async () => {
const ids = ['1', '2', '3', '4', '5'];
const results = await client.$transaction(async (tx) => {
return Promise.all([
...ids.map((id) => tx.user.findUniqueOrThrow({ where: { id } }).posts()),
...ids.map((id) => tx.user.findUniqueOrThrow({ where: { id } }).hat()),
]);
})
console.log(results);
};
run();Note that if I remove the query for hat the batching works as expected.
Expected behavior
I'd expect the batching to work regardless of how many relationships are qeuried.
Prisma information
See reproduction above.
Environment & setup
- OS: macOS Sonoma 14.6.1
- Database: PostgreSQL 11/14
- Node.js version: v20.11.0
Prisma Version
prisma : 5.1.0
@prisma/client : 5.11.0
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine a9b7003df90aa623086e4d6f4e43c72468e6339b (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine : schema-engine-cli a9b7003df90aa623086e4d6f4e43c72468e6339b (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm : @prisma/prisma-schema-wasm 5.1.0-28.a9b7003df90aa623086e4d6f4e43c72468e6339b
Default Engines Hash : a9b7003df90aa623086e4d6f4e43c72468e6339b
Studio : 0.492.0