Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Relationships from imported data.

default discord avatar
jakey___2 years ago
4

I've successfully imported data from a RDB to a payload collection. However, it would probably handier if the fields meant to relate one record to another were actual relation fields instead of just text fields.



For example, each item in my current collection has an XID. This XID is used to relate parent items to child items. Would it be possible to relate items in this way using this XID field directly?

  • discord user avatar
    jmikrut
    2 years ago

    couldn't you just set your

    XID

    field to a relationship type?

  • default discord avatar
    jakey___2 years ago

    is it really that easy? lol. I'll give it a try. Maybe I'm overthinking things

  • discord user avatar
    jmikrut
    2 years ago

    yea haha it might be

  • default discord avatar
    jakey___2 years ago

    Hm, that set me on a path to explore. Will report back when I get a better understanding.



    Ok, so, I changed a field called "ParentID" to a relationship to the same collection it is apart of (

    terms

    in this case).



    Then I adjusted the import code to set the corresponding value in when it creates the document in the collection. Now it's telling me this:



    ValidationError: The following field is invalid: ParentID
        at new ExtendableError (/usr/src/app/node_modules/payload/src/errors/APIError.ts:26:11)
        at new APIError (/usr/src/app/node_modules/payload/src/errors/APIError.ts:43:5)
        at new ValidationError (/usr/src/app/node_modules/payload/src/errors/ValidationError.ts:8:5)
        at beforeChange (/usr/src/app/node_modules/payload/src/fields/hooks/beforeChange/index.ts:52:11)
        at processTicksAndRejections (node:internal/process/task_queues:95:5)
        at create (/usr/src/app/node_modules/payload/src/collections/operations/create.ts:174:29) {
      status: 400,
      data: [
        {
          message: 'This field has the following invalid selections: 96FD1656-02E7-44FB-9B68-E5F3699B1720 0',
          field: 'ParentID'
        }
      ],
      isPublic: false,
      isOperational: true
    }


    the message it's outputting is a little odd.

    96FD1656-02E7-44FB-9B68-E5F3699B1720

    is the id in the db, but that extra

    0

    being appended doesn't make much sense to me.



    i'm guessing that last

    0

    is the index of the row the id is found at for now..



    Ah, i think i see it. I overrode the default id by following this

    https://payloadcms.com/docs/fields/overview#customizable-id

    so I adjusted it from

    TermID

    to

    id

    in my collection config. then also made sure the

    TermID

    was mapped to

    id

    instead when i create the doc



    I think this is working for me. Thanks

    @364124941832159242

    !

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.