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
25.57k stars 1.63k forks source link

Postgres Adapter: Menu field with localized entries produces error on insert #7695

Closed HappyEmu closed 1 month ago

HappyEmu commented 3 months ago

Link to reproduction

https://github.com/HappyEmu/payload-30-postgres-menu-reprod

Payload Version

3.0.0-beta.79

Node Version

v20.9.0

Next.js Version

15.0.0-canary.104

Describe the Bug

Using Postgres Adapter: When using a localized field in an array that is part of another array, an error is thrown when inserting a second entry.

Let's start with adding localization to the Payload config and localize the provided link field definition by adding localized: true to a links label. Then, for example in the posts collection, we add the following field definition to create a menu

{
  name: 'menu',
  type: 'array',
  fields: [
    {
      name: 'items',
      type: 'array',
      fields: [
        link({
          appearances: false,
        }),
      ],
      maxRows: 6,
    },
  ],
}

I know this does not make sense by itself, but just imagine adding a title field to each menu as well (which is what I wanted to do in my app). This is just to provide the smallest possible reproduction.

When I add one menu with one link to a post, everything works. But when I insert a second menu with one link, an error is thrown:

[10:07:08] ERROR: There was an error while saving a version for the Post with ID 1.
[10:07:08] ERROR: Cannot read properties of undefined (reading '_posts_v_version_menu_items_locales_locale_parent_id_unique')
    err: {
      "type": "TypeError",
      "message": "Cannot read properties of undefined (reading '_posts_v_version_menu_items_locales_locale_parent_id_unique')",

I already checked if it has to do with the generated table, column or index name lengths, but the bug happens even with custom shorter dbName overrides.

The setup works perfectly with the Mongo DB Adapter.

BTW: I wanted to do this for the header and footer globals, and it does not work for globals either when versions are enabled.

Reproduction Steps

Adapters and Plugins

db-postgres

r1tsuu commented 1 month ago

Fixed in https://github.com/payloadcms/payload/pull/8331

github-actions[bot] commented 1 month ago

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

github-actions[bot] commented 1 month ago

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