Skip to content

D1 SQLite adapter fails with "too many SQL variables" error on UPDATE operations for large schemas #14766

Open

Description

@billrafferty

Describe the Bug

Description

The @payloadcms/db-d1-sqlite adapter throws a SQLITE_ERROR: too many SQL variables error when attempting to save (update) documents in collections with large schemas. This occurs because SQLite has a hard limit on the number of bound parameters in a single SQL statement (typically 999 or 32766), and the D1 adapter generates UPDATE statements that exceed this limit for complex collections.

While the adapter implements limitedBoundParameters: true and batches INSERT operations to work around this limitation, UPDATE operations are not batched, causing saves to fail on large schemas.

Additional Context

This issue affects code migrations from PostgreSQL to D1, as PostgreSQL doesn't have this parameter limit. The same collection configuration works fine with @payloadcms/db-postgres but fails with @payloadcms/db-d1-sqlite.

The error occurs on both manual saves and autosave operations, making it impossible to use collections with large schemas on D1 without the blocksAsJSON workaround.

Environment

  • Payload Version: 3.63.0
  • Database Adapter: @payloadcms/db-d1-sqlite version 3.63.0
  • Node Version: v20.x
  • Operating System: macOS
  • Deployment: Cloudflare Workers with D1

Collection Configuration

The collection has approximately 200+ fields organized into nested groups and arrays:

  • Multiple nested group fields with sub-fields
  • 5 array fields with complex objects
  • Various relationship fields
  • Custom display fields for calculations

Example structure (simplified):

{
  slug: 'properties',
  fields: [
    {
      name: 'info',
      type: 'group',
      fields: [
        // ~50 fields here
        {
          name: 'agentNotes',
          type: 'array',
          fields: [/* multiple fields */]
        },
        {
          name: 'images',
          type: 'array',
          fields: [/* multiple fields */]
        },
        // ... more arrays
      ]
    },
    {
      name: 'financials',
      type: 'group',
      fields: [/* ~40 fields */]
    },
    {
      name: 'dueDiligence',
      type: 'group',
      fields: [/* ~30 fields with arrays */]
    },
    // ... more groups
  ]
}

Analysis

Looking at the D1 adapter source code:

INSERT operations are batched in insert.ts:

if (this.limitedBoundParameters && Array.isArray(values)) {
  const results: Record<string, unknown>[] = []
  const colsPerRow = Object.keys(values[0]).length
  const maxParams = 100
  const maxRowsPerBatch = Math.max(1, Math.floor(maxParams / colsPerRow))
  // ... batching logic
}

UPDATE operations are NOT batched - they generate a single SQL statement with all fields

The test file sqlite-bound-parameters-limit.int.spec.ts only tests WHERE clause batching for queries, not UPDATE operations

Potential Solutions

-Implement batched UPDATE operations similar to INSERT operations
-Break large UPDATE statements into multiple smaller UPDATEs (update subsets of columns)
-Better document the limitation and recommend blocksAsJSON: true for large schemas

Workarounds

The current workaround is to use blocksAsJSON: true in the adapter configuration:
db: sqliteD1Adapter({   binding: cloudflare.env.D1,  blocksAsJSON: true, // Reduces SQL parameters by storing arrays as JSON})
However, this requires a migration and changes the data structure, which may not be desirable for all use cases.

Related Code
index.ts - Sets limitedBoundParameters: true
insert.ts - Implements batching for INSERTs
packages/drizzle/src/updateOne.ts and updateMany.ts - Do not implement batching
sqlite-bound-parameters-limit.int.spec.ts - Tests only query batching

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/main/test/database/sqlite-bound-parameters-limit.int.spec.ts#L0-L30

Reproduction Steps

Steps to Reproduce

  1. Create a collection with a large number of fields (in my case, ~200+ fields including nested groups and arrays)
  2. Create a document in this collection
  3. Attempt to edit and save the document
  4. Observe the error: Error: too many SQL variables at offset XXXX: SQLITE_ERROR

Expected Behavior

UPDATE operations should be batched similar to INSERT operations when limitedBoundParameters: true, allowing documents in large collections to be saved successfully.

Actual Behavior

The save operation fails with:
caused by: Error: too many SQL variables at offset 3604: SQLITE_ERROR
at D1DatabaseSessionAlwaysPrimary._sendOrThrow (cloudflare-internal:d1-api:140:24)
at async cloudflare-internal:d1-api:374:36
PATCH /api/properties/3?depth=0&draft=true&autosave=true&locale=undefined 500

Which area(s) are affected? (Select all that apply)

db: d1-sqlite

Environment Info

> cross-env NODE_OPTIONS=--no-deprecation payload info

npm warn Unknown env config "npm-globalconfig". This will stop working in the next major version of npm.
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm.
npm warn Unknown env config "_jsr-registry". This will stop working in the next major version of npm.

Binaries:
  Node: 25.2.1
  npm: 11.6.2
  Yarn: 1.22.22
  pnpm: 10.23.0
Relevant Packages:
  payload: 3.63.0
  next: 15.4.7
  @payloadcms/db-d1-sqlite: 3.63.0
  @payloadcms/drizzle: 3.63.0
  @payloadcms/email-resend: 3.63.0
  @payloadcms/graphql: 3.63.0
  @payloadcms/live-preview: 3.63.0
  @payloadcms/live-preview-react: 3.63.0
  @payloadcms/next/utilities: 3.63.0
  @payloadcms/plugin-cloud-storage: 3.63.0
  @payloadcms/plugin-form-builder: 3.63.0
  @payloadcms/plugin-nested-docs: 3.63.0
  @payloadcms/plugin-redirects: 3.63.0
  @payloadcms/plugin-search: 3.63.0
  @payloadcms/plugin-seo: 3.63.0
  @payloadcms/richtext-lexical: 3.63.0
  @payloadcms/storage-r2: 3.63.0
  @payloadcms/translations: 3.63.0
  @payloadcms/ui/shared: 3.63.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:45 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Nov 30, 2025
  2. removed
    validate-reproduction
    on Nov 30, 2025
  3. billrafferty commented on Nov 30, 2025

    @billrafferty
    Author

    The D1 adapter has limitedBoundParameters: true which I think batches INSERT, but UPDATE isn't batched yet…

    There is NO arraysAsJSON option. blocksAsJSON only affects the blocks field type from the Blocks API, not array fields.

    I’d prefer feature to Add UPDATE Batching, but Should I could try and convert array fields to blocks fields or alternativley moving arrays to separate collections?

  4. self-assigned this
    on Nov 30, 2025
  5. removed
    status: needs-triage
    on Nov 30, 2025
  6. billrafferty commented on Dec 8, 2025

    @billrafferty
    Author

    Hi @r1tsuu

    Thanks for picking this up. I’m afraid this is not my area of expertise but is there anything I can contribute to help with this issue?

    Happy to help test, provide more details on the codebase changes needed, or contribute a PR if that would be useful. Just want to avoid duplicating or overcomlicating work if you're already planning a specific approach.

    For now, Do you recomend working around it by converting array fields to blocks, it's a breaking change for our schema but apreciate this might not be a priority so trying to self manage expections.

  7. billrafferty commented on Feb 25, 2026

    @billrafferty
    Author

    Hi @r1tsuu, just wanted to check if this is going to be looked at? If I know it's an acknowledged bug or feature request, I can do some long-term planning. I'm using PostgreSQL to work around it but would really like to have confidence in the SQLite adapter as a lighter platform build.

  8. billrafferty commented on Mar 9, 2026

    @billrafferty
    Author

    Update as of March 2026:

    Detailed technical assessment

    After reviewing the current state of the codebase on main, here is what I found regarding the "too many SQL variables" issue with D1/SQLite UPDATEs on large schemas:

    Summary of findings:

    • The D1 SQLite adapter does set limitedBoundParameters: true.
    • Batching is correctly implemented for INSERTs (see insert.ts), but UPDATE paths still issue a single .update().set(row) call — which hits SQLite's ~100 variable limit if you have a collection with >100 fields.
    • In upsertRow/index.ts, none of the UPDATE paths (optimized or not) break large updates into multiple statements. The same pattern exists if you POST with a large data object or use onConflictDoUpdate through the insert logic for an update: there is no per-column subset update batching.
      • See lines 101–104, 150–154, and 168–171 for the single .update().set(row) pattern.
    • Test coverage for this is lacking: The tests in sqlite-bound-parameters-limit.int.spec.ts focus on WHERE queries and not UPDATE statements with large row payloads.

    What a possible fix would look like

    • On UPDATE (and onConflictDoUpdate), use a helper that splits the update into multiple sequential statements, each updating ≤99 columns per query (accounting for 1 variable for the id in the WHERE clause).
    • Repeat until all columns are updated.
    • See the proposed utility sketch below (for illustration only):
    // Sketch of batched update logic
    for (const chunk of chunkColumns(row, maxColsPerBatch)) {
      await db.update(table).set(chunk).where(eq(idColumn, id))
    }
    • Implement batching at all current UPDATE sites where a large row update can occur.

    Why this matters

    • Current behavior will fail for any large collection update, even if everything else works, and it's currently a hard limitation for users migrating from Postgres to D1.

    Question for @r1tsuu or maintainers;
    Would building out this fix into a pull request be helpful, or do you already have a plan or WIP branch to address this in a different way? I'd be happy to discuss or share a PR sketch if that's best, but I don't want to duplicate effort if something is already underway or would pull your efforts in the wrong direction.

    Thank you for any guidance! Happy to help however is most useful.


    TL;DR

    • The bug is confirmed and not yet fixed for UPDATEs in D1/SQLite: batching only applies to INSERTs, not updates.
    • Implementing batched UPDATEs for large row payloads should solve the issue.
    • Please advise if a PR would be welcomed or if it is already being handled.
  9. dejan-velimirovic-calendly commented on Mar 17, 2026

    @dejan-velimirovic-calendly

    We also experience this issue, however for delete queries

  10. odoyie commented on Mar 20, 2026

    @odoyie

    Any updates on this?, We also experiencec this issue, on updating collection with huge fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions