Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join field causing 500 error with hasMany field #8630

Closed
hsb-tonmoy opened this issue Oct 10, 2024 · 3 comments
Closed

Join field causing 500 error with hasMany field #8630

hsb-tonmoy opened this issue Oct 10, 2024 · 3 comments
Assignees

Comments

@hsb-tonmoy
Copy link

Link to reproduction

No response

Environment Info

Binaries:
Node: 18.20.3
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
payload: 3.0.0-beta.112
next: 15.0.0-canary.173
@payloadcms/db-postgres: 3.0.0-beta.112
@payloadcms/email-nodemailer: 3.0.0-beta.112
@payloadcms/email-resend: 3.0.0-beta.112
@payloadcms/graphql: 3.0.0-beta.112
@payloadcms/next/utilities: 3.0.0-beta.112
@payloadcms/plugin-cloud: 3.0.0-beta.112
@payloadcms/plugin-cloud-storage: 3.0.0-beta.112
@payloadcms/richtext-lexical: 3.0.0-beta.112
@payloadcms/storage-s3: 3.0.0-beta.112
@payloadcms/translations: 3.0.0-beta.112
@payloadcms/ui/shared: 3.0.0-beta.112
react: 19.0.0-rc-3edc000d-20240926
react-dom: 19.0.0-rc-3edc000d-20240926
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Available memory (MB): 32682
Available CPU cores: 20

Describe the Bug

import type { CollectionConfig } from 'payload'
import { slugField } from '../fields/slug'

export const KnowledgebaseCategory: CollectionConfig = {
  slug: 'knowledgebase-category',
  admin: {
    useAsTitle: 'name',
    defaultColumns: ['name', 'slug', 'parent'],
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
      localized: true,
    },
    ...slugField('name'),
    {
      name: 'parent',
      type: 'relationship',
      relationTo: 'knowledgebase-category',
      localized: true,
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'relatedPosts',
      type: 'join',
      collection: 'knowledgebase',
      on: 'category',
    },
  ],
}

I just updated to 3.0.0-beta.112 and tried adding a join field to my categories collection. It has a corresponding hasMany field in the knowledgebase collection. I get the following error when trying to create a new category:

 ⨯ node_modules\.pnpm\@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4\node_modules\@payloadcms\drizzle\dist\find\traverseFields.js (189:54) @ eval
 ⨯ TypeError: Cannot read properties of undefined (reading 'parent')
    at Array.forEach (<anonymous>)
digest: "626849322"
  187 |                                 {
  188 |                                     type: 'innerJoin',
> 189 |                                     condition: and(eq(adapter.tables[joinTable].parent, adapter.tables[joinTableName].id), eq(sql.raw(`"${joinTable}"."${topLevelTableName}_id"`), adapter.tables[currentTableName].id)),
      |                                                      ^
  190 |                                     table: adapter.tables[joinTable]
  191 |                                 }
  192 |                             ];
ERROR: TypeError: Cannot read properties of undefined (reading 'parent')
    at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4/node_modules/@payloadcms/drizzle/dist/find/traverseFields.js:199:171)
    at Array.forEach (<anonymous>)
    at traverseFields (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4/node_modules/@payloadcms/drizzle/dist/find/traverseFields.js:17:12)
    at buildFindManyArgs (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4/node_modules/@payloadcms/drizzle/dist/find/buildFindManyArgs.js:58:71)
    at upsertRow (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4/node_modules/@payloadcms/drizzle/dist/upsertRow/index.js:364:103)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.create (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_mon_rl63tbtcsdhzyugmfdidk7abs4/node_modules/@payloadcms/drizzle/dist/create.js:13:20)
    at async createOperation (webpack-internal:///(rsc)/./node_modules/.pnpm/[email protected][email protected][email protected][email protected]_d23sbrygwbvyo67fjyjnxxfbk4/node_modules/payload/dist/collections/operations/create.js:175:19)
    at async Object.create (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected][email protected][email protected][email protected]_re_yws4hvhcuzu3cwnyo7gsizagv4/node_modules/@payloadcms/next/dist/routes/rest/collections/create.js:25:15)
    at async eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@[email protected][email protected][email protected][email protected]_re_yws4hvhcuzu3cwnyo7gsizagv4/node_modules/@payloadcms/next/dist/routes/rest/index.js:491:19)

NOTE: Error goes away if I remove the join field

Reproduction Steps

  • Create collections named "posts" and "categories"
  • Create a many-to-many relationship between posts and categories:
{
      name: 'category',
      type: 'relationship',
      relationTo: 'categories',
      hasMany: true,
      localized: true,
      admin: {
        position: 'sidebar',
      },
    },
  • Add a join field to the categories collection and a "parent" field of one-to-one relationship.
    {
      name: 'parent',
      type: 'relationship',
      relationTo: categories',
      localized: true,
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'relatedPosts',
      type: 'join',
      collection: 'posts',
      on: 'category',
    },

Adapters and Plugins

No response

@hsb-tonmoy hsb-tonmoy added status: needs-triage Possible bug which hasn't been reproduced yet v3 labels Oct 10, 2024
@r1tsuu r1tsuu self-assigned this Oct 10, 2024
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Oct 10, 2024
DanRibbens pushed a commit that referenced this issue Oct 10, 2024
Fixes #8630

- Fixes `hasMany: true` and `localized: true` on the foreign field
- Adds `limit` to the subquery instead of hardcoded `11`.
- Adds the schema path `field.on` to the subquery, without this having 2
or more relationship fields to the same collection breaks joins
- Properly checks if the field is `hasMany`
@r1tsuu
Copy link
Member

r1tsuu commented Oct 10, 2024

We didn't handle localized: true, fixed here #8633

@r1tsuu r1tsuu closed this as completed Oct 10, 2024
Copy link
Contributor

🚀 This is included in version v3.0.0-beta.113

Copy link
Contributor

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants