Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
On
@payloadcms/db-d1-sqlite, saving a document in any collection whose main table has more than 50 columns fails withD1_ERROR: too many SQL variables. The SQLite adapter'supsertRowupdate path issuesINSERT ... ON CONFLICT (id) DO UPDATE SET, which binds every column twice - once inVALUESand once in theSETclause - so a single-row upsert on a 51-column table binds 102 parameters and overflows D1's limit of 100 bound parameters per statement. Because D1 has no transactions, the failure also lands after related rows (e.g. versions) have already been written.Refs #14766 (the >50-column upsert case; tables wider than 100 columns still need update batching, as discussed there).
How?
In
packages/drizzle/src/sqlite/insert.ts, when the adapter setslimitedBoundParameters(the D1 adapter),ON CONFLICT ... DO UPDATE SETentries that correspond to inserted columns are rewritten to reference SQLite'sexcludedtable ("col" = excluded."col") instead of binding the value a second time. Each column is then bound exactly once (inVALUES), halving the parameter count while keeping the upsert semantics - including theDO UPDATE ... WHEREclause - identical. Values that are alreadySQLfragments, and columns not present in the inserted row(s), are left bound as-is.Measured with drizzle-orm 0.45.2: a 51-column upsert goes from 102 to 51 bound parameters, and the generated
UPDATEresults are byte-identical in all branches (update, insert-when-missing,WHEREno-op).Tests
New
packages/drizzle/src/sqlite/insert.spec.tssimulates the D1 limit by wrapping the libsql client'sexecuteto throw above 100 bound parameters, on a 51-column table:DO UPDATE ... WHEREstill no-ops when the condition does not matchAll 4 tests fail with
too many SQL variablesbefore the fix and pass after. The fullpackages/drizzleunit suite passes (113 tests). The equivalent mitigation for this root cause has been running in production on a D1-backed Payload site.Notes
SETclause; withSETserved fromexcluded, those keys no longer consume statement parameters at all, so the two changes compose.limitedBoundParameters) is a different code path and is not addressed here.