Skip to content

plugin-nested-docs: duplicate breadcrumb IDs across locales cause unique constraint violation on publish #16054

Open

Description

@fkkehlet

Describe the Bug

When using @payloadcms/plugin-nested-docs with localized breadcrumbs and PostgreSQL, publishing a document that has breadcrumbs in multiple locales can fail with:

ValidationError: The following field is invalid: id
"Value must be unique"

The underlying PostgreSQL error is:

duplicate key value violates unique constraint "pages_breadcrumbs_pkey"
Key (id)=(69c3b6df18d83ffbcc032db3) already exists.

Root Cause

In formatBreadcrumb.js, the existing breadcrumb is spread into the new one:

return {
    ...breadcrumb || {},  // carries over the existing `id`
    doc: lastDoc.id,
    label,
    url
};

And in populateBreadcrumbs.js, the breadcrumb is read from the current document at index i:

breadcrumb: currentDocument[breadcrumbsFieldName]?.[i],

The beforeChange hook runs once per save. When Payload publishes, it writes all locales. Since breadcrumbs are localized, each locale gets its own row in a _breadcrumbs table with id as the primary key. But formatBreadcrumb preserves the id from the spread, so if one locale's breadcrumb was initially seeded from another locale's data, both end up with the same id. When Payload inserts both rows in the same transaction, the second insert violates the primary key constraint.

Expected Behavior

Each breadcrumb row should receive a unique id, regardless of locale. formatBreadcrumb should generate a fresh id instead of inheriting it from the spread.

Workaround

Add a beforeChange hook on the collection that forces unique IDs on breadcrumbs:

({ data }) => {
  if (Array.isArray(data?.breadcrumbs)) {
    for (const crumb of data.breadcrumbs) {
      if (crumb && typeof crumb === 'object') {
        crumb.id = [...Array(24)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')
      }
    }
  }
  return data
},

Link to the code that reproduces this issue

https://github.com/fkkehlet/payload-nested-docs-breadcrumb-bug

Reproduction Steps

I observed that two locales ended up with the same breadcrumb id in production, causing a unique constraint violation on publish.

I was unable to reliably reproduce the exact admin UI sequence that causes this, but the underlying issue looks to be that formatBreadcrumb preserves the id from the existing breadcrumb via object spread, and the pages_breadcrumbs table requires unique id values across all rows regardless of locale.

If two locales ever share a breadcrumb id for any reason, publishing becomes impossible.

The integration test forces this state to demonstrate the failure. The suggested fix is for formatBreadcrumb to always generate a fresh id rather than inheriting it from the spread.

  1. Create a Payload project with @payloadcms/plugin-nested-docs, PostgreSQL, and at least two locales
  2. Clone the linked repo, run pnpm install, set up a PostgreSQL database, and configure .env
  3. Run pnpm test:int — the test creates a page, forces both locales to share a breadcrumb ID, then fails on publish with "The following field is invalid: id" / "Value must be unique"

Which area(s) are affected?

plugin: nested-docs, db: postgres

Environment Info

Payload: 3.79.0
Next.js: 15.5.12
Node: 22.22.0
@payloadcms/plugin-nested-docs: 3.79.0
@payloadcms/db-postgres: 3.79.0
@payloadcms/drizzle: 3.79.0
React: 19.2.4
OS: Linux x64

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Mar 25, 2026
  2. added
    db: postgres
    plugin: nested-docs
    and removed
    validate-reproduction
    on Mar 25, 2026
  3. ArjunCodess commented on Sep 19, 2026

    @ArjunCodess

    hi, i'd like to work on this.

    i'd make formatBreadcrumb generate a fresh id per locale instead of spreading the previous one. publish should not hit pages_breadcrumbs_pkey.

    should the id be regenerated on every save, or only when a locale is cloned from another?

  4. lucaspretti commented on Oct 6, 2026

    @lucaspretti

    Reproduced on 3.88.0 with @payloadcms/db-postgres (Postgres 16), localization en/de/fr with fallback: true, drafts with autosave, and the plugin's default localized breadcrumbs. Two published pages are enough: a parent and a child, both created in en; then saving the parent in de fails with "One or more children are invalid" (the underlying error is the pages_breadcrumbs_pkey clash described above).

    Where the duplicate id comes from. resaveChildren fetches the children with locale: req.locale but without fallbackLocale: false. A child that has no de breadcrumbs yet comes back with the en rows through the fallback, row ids included, and that data is written back in de. formatBreadcrumb then keeps the id (on purpose since #13628), and the de insert collides with the en row. Same anatomy as #18002, where copyDataFromLocale fetches without fallbackLocale: false.

    A second effect of the same fetch. Because the whole fallback document is written back in the target locale, the child's English title and slug end up stored as its German values. Untranslated children then look translated. A plain update with empty data in de on a collection without the plugin does not do this, so it comes from the re-save.

    @ArjunCodess, on your question about regenerating the id: stripping the id unconditionally in formatBreadcrumb does stop the clash (verified), but it goes against #13628, which needs row ids to stay stable across form-state round trips, and it does not stop the English values being copied into de.

    A fix at the source that worked here. Add fallbackLocale: false to the two req.payload.find calls in resaveChildren (drafts and published). With that change and nothing else, on the same two pages:

    • the parent saves in de;
    • the child's own de title stays empty (nothing is copied from en);
    • the child's de breadcrumb is built from the parent's German slug, e.g. /sonde-.../probe-child.

    I'm happy to open a PR with that change and an integration test (Postgres, localized breadcrumbs, drafts) unless you already have one in progress. Let me know.

  5. fkkehlet commented on Oct 6, 2026

    @fkkehlet
    Author

    Hey! I don't have any PRs open for this.

  6. added a commit that references this issue on Oct 6, 2026
    10dec65
  7. ArjunCodess commented on Oct 7, 2026

    @ArjunCodess

    i don't have a pr either. fallbackLocale: false on those two finds is the right fix. stripping the id would fight #13628 and still copy the english title into de.

    go ahead and send it.

  8. lucaspretti commented on Oct 7, 2026

    @lucaspretti

    Thanks, both of you. It's up as #18582: fallbackLocale: false on both finds in resaveChildren, with tests on Postgres, SQLite and MongoDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPayload OSSdb: postgresplugin: nested-docsstatus: needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions