Iām trying to create a new content record (a country name) and link it to another content record (a continent) using the API.
Iām getting the following error message:
Failed to create country āNigeriaā: {ādataā:[{āidā:ā7634b9ā,ātypeā:āapi_errorā,āattributesā:{ācodeā:āINVALID_FORMATā,ādetailsā:{āmessagesā:[ā#/data/relationships: failed schema #/definitions/item/links/3/schema/properties/data/properties/relationships: "continent" is not a permitted key.ā]}}}]}
Iām using an HTTP POST request with the json structure shown below. Is this json the correct format for what Iām trying to do?
Many thanks
Tom
data = {
"data": {
"type": "item",
"attributes": {
"country": country_name # Matches 'country' field in your Country model
},
"relationships": {
"item_type": {
"data": {
"type": "item_type",
"id": COUNTRY_MODEL_ID, # ID of the Country model
}
},
"continent": { # 'continent' field in the country content model
"data": {
"type": "item", # The 'item' type for the linked continent
"id": continent_id, # ID of the continent being linked
}
},
},
}
}
Your JSON is almost right Could you please try this instead:
{
"data": {
"type": "item",
"attributes": { // These are the record fields
"country": "New Zealand", // Single-line string
"continent": "KOa9I8zMS4qB3Sgb8luQAg" // ID of the referenced continent, in this case "Zealandia"
},
"relationships": { // This tells our API what you're creating
"item_type": {
"data": {
"type": "item_type",
"id": "fI3EdreaTJ6Xp9UKmZIqTQ" // ID of "country" model, NOT "continent"
}
}
}
}
}
Generally speaking:
Relationships require the record ID of the reference
They are a property of the relationship field (i.e. attributes.continent), not the relationships property (I know itās confusing, sorry!)
The relationships are basically just metadata, and not used to actually link records to each other. They more accurately describe schema relationships (i.e., it tells our API that this request is meant for the country model). To link records to each other, you specify them in fields instead.
Compared to making raw HTTP calls, the JS client handles a lot of the ambiguities for you. The docs for the client are also a lot better for now (weāre slowly working on the HTTP ones, but generally the client is a lot easier to use so we try to focus on that).
Hope that helps! Please let me know if anything was unclear.
Also, protip: The Dato UI itself uses the CMA. So if you ever are unclear about how something is done, just open the network tab in your browser inspector and record the XHR calls right before you hit āsaveā.
Like if you make a new country record in the admin area UI, link it to continent, and then open the network tab before you hit Save, youāll see the exact CMA request needed to create that record. Then you can export it as curl (for Postman/Curl/etc) or fetch (for your backend) and modify accordingly.
Or just ask us! Happy to explain anything and/or update docs