Create record get error: data: failed schema #/definitions/item/links/3/schema/properties/data: \"relationships\" wasn't supplied

Hi team,
Yesterday I tried to remove old datocms-client and use new datocms/cma-client-node 3.1.1
I met a api error when trying to create a new record, here’s my code:

async function createRecord() {
  const record = await client.items.create({
    itemType: { id: '38141', type: 'item_type' },
    pageType: 'about',
    slug: 'test-page-a5b18e08-6b05-4501-a5b3-bf523e9d8e32',
    region: ['2812336'],
    internalId: 'ff3fb769-fbff-4c69-9567-6fc00e96ba6c',
    title: {
      en: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
      de: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
      fr: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
      it: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
      es: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885'
    }
  })
}

createRecord()

and the error message is like below:

ApiError: POST https://site-api.datocms.com/items: 422 Unprocessable Entity

[
  {
    "id": "2431af",
    "type": "api_error",
    "attributes": {
      "code": "INVALID_FORMAT",
      "details": {
        "messages": [
          "#/data: failed schema #/definitions/item/links/3/schema/properties/data: \"relationships\" wasn't supplied."
        ]
      }
    }
  }
]

Does anyone know why this happened? Thanks!

Wow, this took me waaaaaay too long to figure out… :man_facepalming:

In the CMA, field names are underscores and not camelCase like in the GraphQL API.

So instead of itemType, you need item_type. Same for page_type and internal_id. This should work:

async function createRecord() {
    const record = await client.items.create({
        item_type: {type: 'item_type', id: '38141'}, // underscore
        page_type: 'about', // underscore
        slug: 'test-page-a5b18e08-6b05-4501-a5b3-bf523e9d8e32',
        region: ['2812336'],
        internal_id: 'ff3fb769-fbff-4c69-9567-6fc00e96ba6c', // underscore
        title: {
            en: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
            de: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
            fr: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
            it: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885',
            es: 'test-b8d3afcc-2542-45e6-aa8a-d64fc1943885'
        }
    })
}

createRecord()

Talk about subtle gotchas… sorry about that!

Hi roger,
Thanks for your time and help! It works now!

1 Like