Skip to content

Bug: [D1] Error in performIO: Error: D1_ERROR: too many SQL variables #23743

@hrueger

Description

@hrueger

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions