payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.77k stars 1.53k forks source link

Join Field: Incorrect Parent Relationship Assignment When Adding a New Document #8388

Open tobiasiv opened 2 weeks ago

tobiasiv commented 2 weeks ago

Link to reproduction

No response

Environment Info

Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 9.7.0
Relevant Packages:
  payload: 3.0.0-beta.108
  next: 15.0.0-canary.160
  @payloadcms/db-mongodb: 3.0.0-beta.108
  @payloadcms/db-postgres: 3.0.0-beta.108
  @payloadcms/graphql: 3.0.0-beta.108
  @payloadcms/next/utilities: 3.0.0-beta.108
  @payloadcms/richtext-lexical: 3.0.0-beta.108
  @payloadcms/richtext-slate: 3.0.0-beta.108
  @payloadcms/translations: 3.0.0-beta.108
  @payloadcms/ui/shared: 3.0.0-beta.108
  react: 19.0.0-rc-5dcb0097-20240918
  react-dom: 19.0.0-rc-5dcb0097-20240918
Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 19.6.0: Tue Feb 15 21:39:11 PST 2022; root:xnu-6153.141.59~1/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 4

Describe the Bug

I'm working with Postgres and using numeric IDs. When adding a new entry to a join-field-table, the parent relationship should be automatically set for the new entry. However, the parent is not being set correctly. When I attempt to save the newly created record, I get the following error: "This relationship field has the following invalid relationships: 1 0." This issue might be related to the ID type, I've experienced a similar error in the past.

Reproduction Steps

  1. Clone payload-3.0-demo
  2. Update package.json as mentioned in PR #8323
  3. Switch the database from MongoDB to Postgres
  4. Add a relationship field to the pages collection
    {
    slug: 'pages',
    admin: {
    useAsTitle: 'title',
    },
    fields: [
    {
      name: 'title',
      type: 'text',
    },
    {
      name: 'content',
      type: 'richText',
    },
    {
      name: 'user',
      type: 'relationship',
      relationTo: 'users',
    },
    ],
    },
  5. Add join field to the users collection
    {
    slug: 'users',
    auth: true,
    access: {
    delete: () => false,
    update: () => false,
    },
    fields: [
    {
      name: 'pages',
      type: 'join',
      collection: 'pages',
      on: 'user',
    },
    ],
    },
  6. Open a user document and click the "Create new Page" button.
  7. A drawer opens to create a new document, but it sets the parent relationship incorrectly. The label of the relationship field appears as "Untitled - ID: 1". If you try to save the document, you get an error.

Adapters and Plugins

No response

akhrarovsaid commented 1 week ago

Hey @tobiasiv,

I was able to reproduce your issue. However, in order to reproduce this issue, you have to disable the drafts: { autosave: <number> } setting in the collection configuration.

In the process of reproducing your issue I stumbled upon two other issues that seem related to the creation of documents from the "Join" type Drawer component.

  1. Conditional fields are late/lazy to re-render. I can confirm this by selecting the Low Impact hero type in the pages collection and seeing that the media field isn't being hidden away.
  2. With draft autosaves the new document is stuck on Saving... and never actually saves a draft or creates a DB entry - likely due to the aforementioned issue where the error reads: This relationship field has the following invalid relationships: {Id} 0, where {Id} is the numerical id of the relation object

I'm attaching two videos to show these issues in practice.

https://github.com/user-attachments/assets/b418aa26-8f6e-4dff-8bb3-fc255ab42f2b

The above video shows a reproduction of this issue, as well as the issue mentioned in (1).

https://github.com/user-attachments/assets/c259f707-ec5a-4534-85ff-480ac7f8801d

This second video shows (2), and requires you have autosave enabled. In the video, I have an autosave interval of 100. No errors were generated.