Skip to content

fix(plugin-nested-docs): read children in their own locale when re-saving breadcrumbs - #18582

Open
lucaspretti wants to merge 2 commits into
payloadcms:mainfrom
lucaspretti:fix/nested-docs-resave-children-fallback-locale
Open

lucaspretti wants to merge 2 commits into
payloadcms:mainfrom
lucaspretti:fix/nested-docs-resave-children-fallback-locale

Conversation

@lucaspretti

Copy link
Copy Markdown

What?

resaveChildren in @payloadcms/plugin-nested-docs now reads a parent's children with fallbackLocale: false, in both the draft and the published find.

Why?

Fixes #16054.

When a parent is saved in a locale where a child has no breadcrumbs yet, resaveChildren fetches that child in the request's locale with the locale fallback. The child comes back with the fallback locale's breadcrumb rows, row ids included, and that data is written back in the target locale:

  • On Postgres the copied ids collide with the fallback locale's own rows (pages_breadcrumbs_pkey), so the save fails with The following field is invalid: id, surfaced as "One or more children are invalid".
  • On SQLite the same collision is a UNIQUE constraint failed query error. resaveChildren rethrows only validation errors, so it just logs this one: the parent save succeeds and the child's breadcrumbs in that locale are never written.
  • MongoDB has no such constraint, so the child stores the fallback locale's rows, ids included.
  • Where the save goes through (MongoDB, SQLite), the fallback locale's other values (for example title and slug) are also stored as the target locale's values, so untranslated children look translated.

How?

With fallbackLocale: false, each child is re-saved with only its own data in that locale. This is how the update operation already reads the document it updates (fallbackLocale: null for an update by id), so the parent's own breadcrumbs were already built that way; the children now follow the same rule.

The alternative discussed in the issue, regenerating the id in formatBreadcrumb, stops the clash but undoes #13628. It also does not stop the fallback values from being copied.

Behavior change

When a child is validated in the locale being saved and has no value there for a required localized field, the parent save now fails with "One or more children are invalid" (#7977) on every adapter. Before, MongoDB and SQLite let it through by copying the fallback locale's value into that locale (SQLite only logging the id clash); Postgres already failed, on the id clash.

The child is validated when the collection has no drafts. With drafts, a child that is not published in that locale is re-saved as a draft, so it is not validated and the parent save succeeds (the first test below).

If you would rather not fail the parent here, resaveChildren could skip such a child and log it, as it did before #7977. Its breadcrumbs in that locale would then be built when the child itself is saved in that locale. I kept #7977's behavior and pinned it with a test so the choice is explicit; happy to switch.

Tests

Two integration tests in test/plugin-nested-docs/int.spec.ts, under localization:

  • Collection with drafts (pages):
    • a published parent and child are created in en;
    • the parent is saved in de;
    • the test asserts the save succeeds, both locales get the right breadcrumb URLs, and the de rows have ids of their own.
  • Collection without drafts (categories, whose name is now localized):
    • a parent and child are created in en;
    • the parent is saved in de;
    • the test asserts the save fails as described above and the child gets no de value copied from en.

Run with pnpm test:int:postgres plugin-nested-docs, pnpm test:int:sqlite plugin-nested-docs and pnpm test:int plugin-nested-docs (MongoDB):

  • without the fix:
    • Postgres: the first test fails with the error from the issue; the second passes, because the same id clash already fails the parent save;
    • SQLite: both fail. The first because the child's de breadcrumbs are never written (the clash is only logged); the second because the save succeeds and the en name is stored as the de one;
    • MongoDB: both fail. The first because the de rows reuse the en row ids; the second as on SQLite;
  • with the fix: 14/14 on all three.

On 3.x, where the bug was reported (3.79, reproduced on 3.88), _status is not per locale, so every published child is validated in the locale being saved and the behavior change above applies to collections with drafts too. I did not open a backport for that reason; happy to if you want one.

…ving breadcrumbs

resaveChildren fetched a parent's children in the request's locale without fallbackLocale: false. A child with no breadcrumbs in that locale came back with the fallback locale's rows, row ids included, and was written back in the target locale: the ids clashed with the fallback locale's rows on Postgres ("The following field is invalid: id"), and the fallback locale's other values were copied into the target locale.

Reading the children with fallbackLocale: false writes only that locale's own data, keeping the row ids stable within a locale (payloadcms#13628).

Fixes payloadcms#16054
Without drafts, each child is validated in the locale being saved. A
child with no value there for a required localized field now fails the
parent save (payloadcms#7977) instead of getting the fallback locale's value
copied into that locale. The categories name is localized so the test
can show it.
@lucaspretti

Copy link
Copy Markdown
Author

Proposing a backport to 3.x, since that is where the bug was reported (3.79) and where most projects are today, ours included (3.90).

A branch is ready on my fork, fix/nested-docs-resave-children-fallback-locale-3.x: the same fallbackLocale: false on both finds in resaveChildren, cherry-picked onto 3.x, with the drafts regression test (Postgres: it fails without the change and the suite passes with it).

The one difference on 3.x, as noted above: _status is not per locale there, so every published child is validated in the locale being saved, and the stricter behavior (#7977, "One or more children are invalid" when a child has no value for a required localized field in that locale) also applies to collections with drafts. On Postgres the parent save already fails today, on the id clash, so nothing gets worse there; on MongoDB and SQLite it replaces silently copying the fallback locale's values into the saved locale.

If you'd like it, I'll open it as a PR against 3.x. If you'd rather keep the 3.x behavior lenient, I can make the backport skip and log such a child instead of failing the parent.

@gary-bc

gary-bc commented Nov 2, 2026

Copy link
Copy Markdown

My project has his this bug! on 3.90.1

@lucaspretti

Copy link
Copy Markdown
Author

Thanks for confirming, @gary-bc. Until this gets reviewed, the same fix on top of 3.x is on my fork: fix/nested-docs-resave-children-fallback-locale-3.x. It is two lines in resaveChildren (fallbackLocale: false on both finds), so it applies cleanly with patch-package; we run it that way on 3.90.2.

Maintainers: happy to open the 3.x backport as a PR if you want it (details in my comment above).

@gary-bc

gary-bc commented Nov 2, 2026

Copy link
Copy Markdown

Thanks for confirming, @gary-bc. Until this gets reviewed, the same fix on top of 3.x is on my fork: fix/nested-docs-resave-children-fallback-locale-3.x. It is two lines in resaveChildren (fallbackLocale: false on both finds), so it applies cleanly with patch-package; we run it that way on 3.90.2.

Maintainers: happy to open the 3.x backport as a PR if you want it (details in my comment above).

Yeah we've patched it currently.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants