payload icon indicating copy to clipboard operation
payload copied to clipboard

nested-docs-plugin error if child has invalid forward draft

Open yobottehg opened this issue 1 year ago • 5 comments

Link to reproduction

Reproduction

Payload Version

3.0.0-beta.68

Node Version

20 LTS

Next.js Version

15.0.0-carary.57

Describe the Bug

When using the nested docs plugin and a parent document is published it tries to resave it's children.

If the children have invalid drafts the plugin fails to do so resulting in a lot of error messages and possible deadlocks (I have both in the logs.)

[12:13:41] [31mERROR[39m: [36mNested Docs plugin has had an error while re-saving a child document as published.[39m
[12:13:41] [31mERROR[39m: [36mThe following field is invalid: content.2.consultant[39m
    err: {
      "type": "l",
      "message": "The following field is invalid: content.2.consultant",
      "stack":
          l: The following field is invalid: content.2.consultant
              ...
      "data": {
        "collection": "pages",
        "errors": [
          {
            "field": "content.2.consultant",
            "message": "This field is required."
          }
        ]
      },
      "isOperational": true,
      "isPublic": false,
      "status": 400,
      "name": "l"
    }

Shouldn't the nested-docs-plugin just re-publish the latest published version of the children instead with the new url instead of creating new published version of their drafts?

Reproduction Steps

  1. Add 2 pages which use the nested docs plugin.
  2. Make one a parent and one a child.
  3. Now create an unpublished draft version of the child.
  4. Go to the parent and change it's slug.
  5. See an error as the child url can not be updated.

Adapters and Plugins

nested-docs, db-postgres

yobottehg avatar Aug 15 '24 07:08 yobottehg

Hey @yobottehg I tested a few workflows here, on the latest beta and next canary 104+, with publishing and draft modes and in both mongo and postgres and I can't reproduce this.

Could you provide a reproduction please? Or more detailed steps?

paulpopus avatar Aug 15 '24 22:08 paulpopus

@paulpopus

I found an easy way to reproduce this also using the demo:

  1. create page 1 with slug "parent"
  2. create page 2 with slug "child" and add an image (but any reference will do)
  3. delete the image via the image collection
  4. go to page 1 and change the slug

I think this goes more into the direction on how to handle deleted references instead of being only a nested docs problem.

yobottehg avatar Aug 16 '24 07:08 yobottehg

Thanks @yobottehg I followed those exact steps in both mongo and postgres on the latest beta and I couldn't get this to error, the referenced category is removed as expected in postgres

Can you test on the latest and let me know if you can still reproduce? Is there anything else related to your setup, eg hooks etc. that run?

paulpopus avatar Aug 20 '24 19:08 paulpopus

@paulpopus I added a reproduction. Only tested locally with postgres.

My steps to reproduce were wrong. Issue only appears if the deleted reference is inside a block.

I added all necessary steps to reproduce to the onInit function here: https://github.com/yobottehg/payload/blob/beta-repro-7694/test/_community/config.ts#L26

yobottehg avatar Aug 21 '24 08:08 yobottehg

Following this since I'm also dealing with this same error

tiago-meireles avatar Aug 21 '24 13:08 tiago-meireles

Will be fixed in the next release via https://github.com/payloadcms/payload/pull/7977

We will throw an error to the UI explaining that the children are no longer passing validation.

In the future we'll look into how deleting references in this manner affects validation of where it's used.

paulpopus avatar Aug 30 '24 02:08 paulpopus

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Sep 06 '24 20:09 github-actions[bot]