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?
couldn't you just set your
XID
field to a relationship type?
is it really that easy? lol. I'll give it a try. Maybe I'm overthinking things
yea haha it might be
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 @jmikrut !
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.