payload icon indicating copy to clipboard operation
payload copied to clipboard

db-d1-sqlite: upsert is aliased to updateOne (no options.upsert) — user preferences silently never persist on D1

Open cshyang opened this issue 5 months ago • 0 comments

Describe the Bug

@payloadcms/db-d1-sqlite aliases the adapter's upsert method directly to updateOne:

// packages/db-d1-sqlite (dist/index.js around L134)
{
  updateOne,
  updateVersion,
  upsert: updateOne,   // ← upsert IS updateOne, with no options.upsert
}

updateOne without options: { upsert: true } performs a plain UPDATE — which matches nothing and returns null without inserting when the target row doesn't yet exist. So on D1, any payload.db.upsert(...) for a not-yet-existing row silently does nothing.

The most visible casualty is user preferences (payload_preferences), which Payload writes via upsert. On D1, admin preferences never persist: collapse a nav group, reorder/select list columns, dismiss things — reload the page and it's all reset, every time. No error is thrown; the write just no-ops. This affects every Payload-on-D1 deployment, not an edge case.

Root cause

upsert: updateOne treats the two as interchangeable, but a real upsert must call updateOne with options: { upsert: true } — which is exactly what the base @payloadcms/drizzle upsert wrapper does. The D1 adapter drops that option.

Fix (verified as a runtime workaround)

Rebinding upsert to pass the option makes preferences persist correctly on D1:

// onInit workaround — the adapter itself should do this
const updateOne = payload.db.updateOne.bind(payload.db)
payload.db.upsert = (args) => updateOne({ ...args, options: { upsert: true } })

Suggested fix in the adapter: assign upsert: (args) => updateOne({ ...args, options: { upsert: true } }) instead of upsert: updateOne.

Reproduction Steps

  1. Payload 3.82.1 with @payloadcms/db-d1-sqlite 3.82.1 on a Cloudflare D1 database.
  2. In the admin, collapse a nav group or change the visible columns on any list view.
  3. Reload the page → the preference is gone. payload_preferences never received the row (the upsert no-op'd).

Environment

  • payload 3.82.1, @payloadcms/db-d1-sqlite 3.82.1
  • Cloudflare D1, deployed via @opennextjs/cloudflare / Workers
  • Reproduces regardless of runtime (it's a query-layer bug, not runtime-specific)

cshyang avatar Jul 04 '26 15:07 cshyang