payload icon indicating copy to clipboard operation
payload copied to clipboard

D1 Adapter: `hasMany: true` select field inside array causes `referencedTable` error

Open Ardnived opened this issue 4 months ago • 0 comments

Describe the Bug

When using a select field with hasMany: true inside an array field, the D1 adapter generates malformed relation metadata, causing a runtime error that prevents the admin panel from loading.

The reproduction I've provided it based on the D1 template, because that's what I've been working with, but I think the error may also applied for the standard sqlite adapter. I did a brief test with that, but not extensively. I have confirmed that the error doesn't occur with Postgres or MongoDB adapters.

ERROR: Cannot read properties of undefined (reading 'referencedTable')
    err: {
      "type": "TypeError",
      "message": "Cannot read properties of undefined (reading 'referencedTable')",
      "stack":
          TypeError: Cannot read properties of undefined (reading 'referencedTable')
              at normalizeRelation (drizzle-orm/relations.js:212:75)
              at SQLiteAsyncDialect.buildRelationalQuery (drizzle-orm/sqlite-core/dialect.js:454:36)
              at SQLiteAsyncDialect.buildRelationalQuery (drizzle-orm/sqlite-core/dialect.js:466:36)
              at QueryPromise._toSQL (drizzle-orm/sqlite-core/query-builders/query.js:116:32)
              at QueryPromise._prepare (drizzle-orm/sqlite-core/query-builders/query.js:95:40)
              at QueryPromise.executeRaw (drizzle-orm/sqlite-core/query-builders/query.js:136:17)
              at QueryPromise.execute (drizzle-orm/sqlite-core/query-builders/query.js:139:17)
              at QueryPromise.then (drizzle-orm/query-promise.js:21:17)
    }

Link to the code that reproduces this issue

https://github.com/Ardnived/payload-d1-hasmany-reproduction

Reproduction Steps

  1. Clone the reproduction repository
  2. Run pnpm install
  3. Run pnpm dev
  4. Navigate to http://localhost:3000/admin/login
  5. Observe the error in the console

The error is triggered by this field configuration in the Users collection:

{
  name: 'permissions',
  type: 'array',
  fields: [
    {
      name: 'locales',
      type: 'select',
      hasMany: true,  // ← This triggers the error
      required: true,
      options: [
        { label: 'English', value: 'en' },
        { label: 'Spanish', value: 'es' },
        // ... more locale options
      ],
    },
  ],
}

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

db: sqlite db: d1-sqlite

Environment Info

Binaries:
  Node: 22.17.0
  npm: 10.9.2
  Yarn: 1.22.22
  pnpm: 9.12.1
Relevant Packages:
  payload: 3.64.0
  next: 15.4.7
  @payloadcms/db-d1-sqlite: 3.64.0
  @payloadcms/drizzle: 3.64.0
  @payloadcms/graphql: 3.64.0
  @payloadcms/next/utilities: 3.64.0
  @payloadcms/plugin-cloud-storage: 3.64.0
  @payloadcms/richtext-lexical: 3.64.0
  @payloadcms/storage-r2: 3.64.0
  @payloadcms/translations: 3.64.0
  @payloadcms/ui/shared: 3.64.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.1.0: Mon Oct 20 19:33:36 PDT 2025; root:xnu-12377.41.6~2/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 11

Ardnived avatar Nov 27 '25 11:11 Ardnived