Repository navigation
Description
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
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.
- Create a Payload project with
@payloadcms/plugin-nested-docs, PostgreSQL, and at least two locales - Clone the linked repo, run
pnpm install, set up a PostgreSQL database, and configure.env - 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
ArjunCodess commentedon Sep 19, 2026
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?
lucaspretti commentedon Oct 6, 2026
Reproduced on 3.88.0 with
@payloadcms/db-postgres(Postgres 16), localizationen/de/frwithfallback: true, drafts with autosave, and the plugin's default localizedbreadcrumbs. Two published pages are enough: a parent and a child, both created inen; then saving the parent indefails with "One or more children are invalid" (the underlying error is thepages_breadcrumbs_pkeyclash described above).Where the duplicate id comes from.
resaveChildrenfetches the children withlocale: req.localebut withoutfallbackLocale: false. A child that has nodebreadcrumbs yet comes back with theenrows through the fallback, row ids included, and that data is written back inde.formatBreadcrumbthen keeps the id (on purpose since #13628), and thedeinsert collides with theenrow. Same anatomy as #18002, wherecopyDataFromLocalefetches withoutfallbackLocale: false.A second effect of the same fetch. Because the whole fallback document is written back in the target locale, the child's English
titleandslugend up stored as its German values. Untranslated children then look translated. A plain update with empty data indeon 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
formatBreadcrumbdoes 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 intode.A fix at the source that worked here. Add
fallbackLocale: falseto the tworeq.payload.findcalls inresaveChildren(drafts and published). With that change and nothing else, on the same two pages:de;detitle stays empty (nothing is copied fromen);debreadcrumb 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.
fkkehlet commentedon Oct 6, 2026
Hey! I don't have any PRs open for this.
fix(plugin-nested-docs): read children in their own locale when re-sa…
ArjunCodess commentedon Oct 7, 2026
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.
lucaspretti commentedon Oct 7, 2026
Thanks, both of you. It's up as #18582:
fallbackLocale: falseon both finds inresaveChildren, with tests on Postgres, SQLite and MongoDB.chore: address six more Payload reports and prepare the fork PR
fix: address 24 Payload read, schema, version and tooling reports (#9)