contentful-management.js
contentful-management.js copied to clipboard
Update references to new content type
I duplicated one of my content types in order to change its id and am now trying to update references from the old to the new type:
export async function updateRefsToNewType() {
// person to author
const space = await contentful.createClient({
accessToken: process.env.CONTENTFUL_MANAGEMENT_TOKEN,
}).getSpace(id)
const env = await space.getEnvironment(`master`)
let { items: persons } = await env.getEntries({
content_type: `1kUEViTN4EmGiEaaeC6ouY`,
})
let { items: authors } = await env.getEntries({ content_type: `author` })
let { items } = await env.getEntries({ content_type: `post` })
items.forEach(async (itm) => {
const authorId = itm.fields.author.de.sys.id
const authorName = persons.find((p) => p.sys.id === authorId).fields.name.de
const newId = authors.find((a) => a.fields.name.de === authorName).sys.id
itm.fields.author.de.sys.id = newId
itm.publish()
})
}
This fails with
InvalidEntry: {
"status": 422,
"statusText": "Unprocessable Entity",
"message": "Validation error",
"details": {
"errors": [
{
"name": "linkContentType",
"details": "Link must point to an Entry with the Content Type author",
...
I assume I'm doing this the wrong way since the id I'm passing actually is one of type author. How do I change a reference on an entry to a new type?
Just saw #247 so I'm guessing this is not possible at all?
Is this possible with the Contentful Migration CLI?
Hey @janosh. it's a pity to see that nobody replied to you earlier. I just wanted to say that based on the error message, your content type post has a validation rule set for one of the fields, which only allows references to other entries of type post. I would take a look at the rules there and then carefully see which of the fields has the reference of the wrong type.
Since this issue is old, I will close it, but please reach out again if you get stuck.
And again sorry that this issue was lost in time.