Trying to migrate some data but cannot update 1-on-1

default discord avatar
falko100
10 months ago
1 2

Bug Report

We are changing a block to have a list of Call to Actions instead of just one.
I'm trying to migrate the cta field into the ctaList field like so:

const pages = await payload.find({
  collection: 'pages',
});

pages.docs.forEach((page: Page) => {
  page.content.forEach((block) => {
    if (block.blockType === Slug.DefaultContent) {
      if (block.cta) {
        block.ctaList = [block.cta];

        payload.update({
          collection: 'pages',
          id: page.id,
          data: {
            content: page.content,
          },
        });
      }
    }
  });
});

But I am getting the follow errors:
ValidationError: followingFieldsInvalid content.0.image.image, content.0.cta.page, content.0.ctaList.0.page
These are all relationship fields that are bugging out. Any tips on how I can do this?

I am also thinking of making a migration plugin for Payload. If anyone cares to help, let me know 😄

Other Details

Payload: 1.2.1

  • discord user avatar
    jmikrut
    Payload Team
    10 months ago

    Well, I think in this case @falko100 the problem is that your relationship field data is not in the right shape.

    Relationships have four potential different data shapes, so if you're trying to fill relationships, you need to determine which shape is right for your field(s) and pass data to the operation according to the required relationship field configuration.

    We have some docs about this here:
    https://payloadcms.com/docs/fields/relationship#how-the-data-is-saved

    We do this just about every day without problem, so I don't think this is a bug - moreso you are just passing data incorrectly. I'll bump this over to a Discussion and we can continue the conversation there!

    2 replies
  • default discord avatar
    falko100
    10 months ago

    Ahh ok, but the data returned isn't structured like:
    {"owner":"abcd-key"}
    but:
    {"id":"abcd-key", doc: {...}}

    Is there a way to use the Local API find to return plain data without the relations loaded?

  • default discord avatar
    falko100
    10 months ago

    After looking at the documentation a bit more, I figured I could add depth: 0, to my Local API call and then it works :)

    I currently made a route in my server.ts file. What's a good location to do these kind of data migrations?

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    Are there additional/required fields defined on your Pages collection? You will likely have to spread the existing page data like this:

    payload.update({
      collection: "pages",
      id: page.id,
      data: {
        ...page,
        content: page.content,
      },
    });

    Let me know if that helps.

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.