prisma: Bug: [D1] Error in performIO: Error: D1_ERROR: too many SQL variables
Bug description
When running queries with relations with a D1 database as the backend, I run into the following error quite often:
Error in performIO: Error: D1_ERROR: too many SQL variables at offset [some offset].
This is probably the same as #302 (but is closed) and #21648. relationJoins does not change anything since that is not available for sqlite/d1.
How to reproduce
Checkout https://github.com/hrueger/prisma-d1-variables-bug and follow the instructions (also in the readme):
npm install
npx wrangler d1 create prisma-demo-db
update the wrangler.toml file with the database id
npx wrangler d1 migrations apply prisma-demo-db --local
npm run dev
Expected behavior
it should work 😉
I believe the split limit (I read about this in some other issue) has to be reduced. Seems like this is 100, see (https://developers.cloudflare.com/d1/platform/limits/)
Prisma information
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
import { PrismaClient } from '@prisma/client'
import { PrismaD1 } from '@prisma/adapter-d1'
export interface Env {
DB: D1Database
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const adapter = new PrismaD1(env.DB)
const prisma = new PrismaClient({ adapter })
let users = await loadUsers(prisma)
if (!users.length) {
await createUsers(prisma);
users = await loadUsers(prisma);
}
const result = JSON.stringify(users)
return new Response(result);
},
};
async function loadUsers(prisma: PrismaClient) {
return await prisma.user.findMany({
include: {
posts: true,
},
});
}
async function createUsers(prisma: PrismaClient) {
for (let i = 0; i < 250; i++) {
await prisma.user.create({
data: {
email: `user${i}@example.com`,
name: `User ${i}`,
posts: {
createMany: {
data: [
{
title: `Post ${i}`,
content: `This is the content of post ${i} by User ${i}`,
},
{
title: `Post ${i}b`,
content: `This is the content of the second post ${i}b by User ${i}`,
},
],
},
},
},
});
}
}
Environment & setup
- OS: macOS
- Database: Cloudflare D1
- Node.js version: v21.6.1
Prisma Version
prisma : 5.12.1
@prisma/client : 5.12.1
Computed binaryTarget : darwin-arm64
Operating System : darwin
Architecture : arm64
Node.js : v21.6.1
Query Engine (Node-API) : libquery-engine 473ed3124229e22d881cb7addf559799debae1ab (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine : schema-engine-cli 473ed3124229e22d881cb7addf559799debae1ab (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm : @prisma/prisma-schema-wasm 5.12.0-21.473ed3124229e22d881cb7addf559799debae1ab
Default Engines Hash : 473ed3124229e22d881cb7addf559799debae1ab
Studio : 0.499.0
Preview Features : driverAdapters
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 17 (7 by maintainers)
Commits related to this issue
- test(query-engine-wasm): add tests for https://github.com/prisma/prisma/issues/23743 and https://github.com/prisma/prisma/issues/23919 — committed to prisma/prisma-engines by jkomyno 2 years ago
- test(query-engine-wasm): add tests for D1 chunking issues (#4905) * test(query-engine-wasm): add tests for https://github.com/prisma/prisma/issues/23743 and https://github.com/prisma/prisma/issues/23... — committed to prisma/prisma-engines by jkomyno 2 years ago
- 🐛 [BUG FIX]: D1でインサートするときにデータサイズが多いとエラーになるので、分割してインサートするようにした NOTE: https://github.com/prisma/prisma/issues/23743#issuecomment-2121768254 — committed to hrkmtsmt/api.hrkmtsmt.me by hrkmtsmt 2 years ago
- 🐛 [BUG FIX]: D1でインサートするときにデータサイズが多いとエラーになるので、分割してインサートするようにした NOTE: https://github.com/prisma/prisma/issues/23743#issuecomment-2121768254 — committed to hrkmtsmt/api.hrkmtsmt.me by hrkmtsmt 2 years ago
- 🐛 [BUG FIX]: D1でインサートするときにデータサイズが多いとエラーになるので、分割してインサートするようにした NOTE: https://github.com/prisma/prisma/issues/23743#issuecomment-2121768254 — committed to hrkmtsmt/api.hrkmtsmt.me by hrkmtsmt 2 years ago
Hi @hrueger! This issue is fixed, and will be part of the upcoming release (in a few hours): Prisma 5.15.0. Thanks!
Just dropping in to say this works brilliantly 🎉
Hey @janpio, I double checked and I’m definitely on 5.15.0, I’ll try to put together an example to reproduce it. Thanks for your help so far.
Quick attempt seems to show the chunking is working as expected for me:
This is with 150 entries and then also reading 150 back - note how we have 1 query with 100 and then 1 with 50.
Are you sure you are on 5.15.0?
I just encountered this error 20 mins ago, then find this great news, upgrade to 5.15.0 and the error is gone. Thank you guys.
@zett-8 yes, apart from this issue it works just fine for me 👍 I have multiple projects running on Cloudflare Workers as well as on Cloudflare Pages.
That would be amazing. Just open a new issue with that information when you get it. The problem definitely seems to happen less often after the previous fix, but it seems we didn’t get all the instances yet. Let’s make that happen.
Hey @janpio I’ve been doing some more digging, it looks like the inclusion of a subquery is what pushes it over the edge. Example like below, I’ll hopefully have a working example on how to reproduce shortly.
So you see queries currently being limited to 100
?and still get the error message? Can you share the exact query that is triggering the error? It is possible that D1 counts differently than … usual, and that is causing trouble. Maybe also share your Prisma Schema and Prisma Client query so we can fully reproduce this. How manyMemberships orUsers does the query return?I think I still get this issue when running a query with a join that has over 100 records. I can see since 5.15.0 the query is being successfully chunked and hopefully this is user error on my part.
When I run the below select as part of a prisma ‘findMany’.
And where the original table, in this case ‘members’ returns over 100 results, I end up with the following queries:
Essentially the inclusion of over 100
?in the query seems to get treated as breaching the ‘100’ D1 variables limit.We have a mechanism called “chunking” that we usually use to split problematic queries to stay inside the limits imposed by the databases. We are currently implementing this for D1, and expect this to fix the problem.
Do you have a concrete example that is currently failing with Prisma ORM, so we can confirm it now works after our fix @TimKieu?