Repository navigation
fix(plugin-nested-docs): read children in their own locale when re-saving breadcrumbs - #18582
lucaspretti wants to merge 2 commits into
Conversation
…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.
|
Proposing a backport to A branch is ready on my fork, The one difference on If you'd like it, I'll open it as a PR against |
|
My project has his this bug! on |
|
Thanks for confirming, @gary-bc. Until this gets reviewed, the same fix on top of Maintainers: happy to open the |
Yeah we've patched it currently. |
What?
resaveChildrenin@payloadcms/plugin-nested-docsnow reads a parent's children withfallbackLocale: false, in both the draft and the publishedfind.Why?
Fixes #16054.
When a parent is saved in a locale where a child has no breadcrumbs yet,
resaveChildrenfetches 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:pages_breadcrumbs_pkey), so the save fails withThe following field is invalid: id, surfaced as "One or more children are invalid".UNIQUE constraint failedquery error.resaveChildrenrethrows only validation errors, so it just logs this one: the parent save succeeds and the child's breadcrumbs in that locale are never written.titleandslug) 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: nullfor an update by id), so the parent's own breadcrumbs were already built that way; the children now follow the same rule.formatBreadcrumbstill keeps row ids stable within a locale, as fix(plugin-nested-docs): prevent phantom breadcrumb row #13628 requires.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,
resaveChildrencould 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, underlocalization:pages):en;de;derows have ids of their own.categories, whosenameis now localized):en;de;devalue copied fromen.Run with
pnpm test:int:postgres plugin-nested-docs,pnpm test:int:sqlite plugin-nested-docsandpnpm test:int plugin-nested-docs(MongoDB):debreadcrumbs are never written (the clash is only logged); the second because the save succeeds and theenname is stored as thedeone;derows reuse theenrow ids; the second as on SQLite;On
3.x, where the bug was reported (3.79, reproduced on 3.88),_statusis 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.