Describe the Bug
When using the @payloadcms/plugin-nested-docs plugin with versioned collections, saving a parent document causes child documents that have unpublished changes (draft versions) to lose their published status. The child document disappears from the live site.
Root Cause
The issue is in the resaveChildren hook (packages/plugin-nested-docs/src/hooks/resaveChildren.ts). When a parent document is published, the hook updates all child documents to refresh their breadcrumbs. However, when a child has both a published version AND a draft version (with draft being newer):
- The hook updates the published version first (
draft: false) - this creates a new published version and sets latest=false on all older versions
- The hook then updates the draft version (
draft: true) - this creates a new draft version which sets latest=false on the just-created published version
The problem is in how Payload's versioning system handles the latest flag in packages/drizzle/src/createVersion.ts:
if (hasDraftsEnabled(collection)) {
await this.execute({
db,
sql: sql`
UPDATE ${table}
SET latest = false
WHERE ${table.id} != ${result.id}
AND ${table.parent} = ${parent}
AND ${table.updatedAt} < ${result.updatedAt}
`,
})
}
Every new version sets latest=false on ALL older versions, regardless of their _status. This causes the published version to become inaccessible.
Link to the code that reproduces this issue
https://github.com/payloadcms/payload/blob/main/packages/plugin-nested-docs/src/hooks/resaveChildren.ts, https://github.com/payloadcms/payload/blob/main/packages/drizzle/src/createVersion.ts, https://github.com/payloadcms/payload/blob/main/packages/payload/src/versions/saveVersion.ts
Reproduction Steps
- Create a collection with
versions.drafts enabled
- Configure the
nested-docs plugin for this collection
- Create Page A (parent document) and publish it
- Create Page B (child document with parent set to Page A) and publish it
- Make unpublished changes to Page B (this creates a draft/autosave version)
- Now Page B has both: a published version AND a newer draft version
- Save/publish Page A
- Bug: Page B's published version is no longer accessible - the page disappears from the live site
Which area(s) are affected?
plugin: nested-docs
Environment Info
> test-bug@1.0.0 payload D:\Projects\test\test-bug
> cross-env NODE_OPTIONS=--no-deprecation payload "info"
Binaries:
Node: 22.17.0
npm: N/A
Yarn: N/A
pnpm: 10.12.4
Relevant Packages:
payload: 3.68.4
next: 15.4.10
@payloadcms/db-postgres: 3.68.4
@payloadcms/drizzle: 3.68.4
@payloadcms/graphql: 3.68.4
@payloadcms/live-preview: 3.68.4
@payloadcms/live-preview-react: 3.68.4
@payloadcms/next/utilities: 3.68.4
@payloadcms/plugin-form-builder: 3.68.4
@payloadcms/plugin-nested-docs: 3.68.4
@payloadcms/plugin-redirects: 3.68.4
@payloadcms/plugin-search: 3.68.4
@payloadcms/plugin-seo: 3.68.4
@payloadcms/richtext-lexical: 3.68.4
@payloadcms/translations: 3.68.4
@payloadcms/ui/shared: 3.68.4
react: 19.2.1
react-dom: 19.2.1
Operating System:
Platform: win32
Arch: x64
Version: Windows 11 Pro
Available memory (MB): 32509
Available CPU cores: 32
Describe the Bug
When using the
@payloadcms/plugin-nested-docsplugin with versioned collections, saving a parent document causes child documents that have unpublished changes (draft versions) to lose their published status. The child document disappears from the live site.Root Cause
The issue is in the
resaveChildrenhook (packages/plugin-nested-docs/src/hooks/resaveChildren.ts). When a parent document is published, the hook updates all child documents to refresh their breadcrumbs. However, when a child has both a published version AND a draft version (with draft being newer):draft: false) - this creates a new published version and setslatest=falseon all older versionsdraft: true) - this creates a new draft version which setslatest=falseon the just-created published versionThe problem is in how Payload's versioning system handles the
latestflag inpackages/drizzle/src/createVersion.ts:Every new version sets
latest=falseon ALL older versions, regardless of their_status. This causes the published version to become inaccessible.Link to the code that reproduces this issue
https://github.com/payloadcms/payload/blob/main/packages/plugin-nested-docs/src/hooks/resaveChildren.ts, https://github.com/payloadcms/payload/blob/main/packages/drizzle/src/createVersion.ts, https://github.com/payloadcms/payload/blob/main/packages/payload/src/versions/saveVersion.ts
Reproduction Steps
versions.draftsenablednested-docsplugin for this collectionWhich area(s) are affected?
plugin: nested-docs
Environment Info