payload icon indicating copy to clipboard operation
payload copied to clipboard

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

Open HappyEmu opened this issue 1 year ago • 0 comments

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

  • Checkout latest Payload 3.0 Website Template (Payload 3.0.0-beta.79)
  • Enable localization in Payload Config
localization: {
  locales: ['en', 'de'],
  defaultLocale: 'en',
  fallback: true,
},
  • Localize label of link
// payload/fields/link.ts
linkResult.fields.push({
  type: 'row',
  fields: [
    ...linkTypes,
    {
      name: 'label',
      type: 'text',
      admin: {
        width: '50%',
      },
      label: 'Label',
      required: true,
      // Step 2: Added this
      localized: true,
    },
  ],
})
  • Add menu field to post collection
// payload/collections/Posts/index.ts
{
  name: 'menu',
  type: 'array',
  fields: [
    {
      name: 'items',
      type: 'array',
      fields: [
        link({
          appearances: false,
        }),
      ],
      maxRows: 6,
    },
  ],
}

Try to save post with 2 menus with 1 entry each. This will fail with above mentioned error.

Adapters and Plugins

db-postgres

HappyEmu avatar Aug 15 '24 08:08 HappyEmu