How to use correct body request for relation?

System Information
  • Strapi Version: 5.0.0
  • Operating System: Ubuntu 20.04
  • Database: Postgres
  • Node Version: 20
  • NPM Version: 10
  • Yarn Version: …

Hi there,

I have a simple code strapi for batch, here is schema of this:
{
“kind”: “collectionType”,
“collectionName”: “batches”,
“info”: {
“singularName”: “batch”,
“pluralName”: “batches”,
“displayName”: “batch”,
“description”: “”
},
“options”: {
“draftAndPublish”: true
},
“pluginOptions”: {},
“attributes”: {
“name”: {
“type”: “string”
},
“state”: {
“type”: “enumeration”,
“enum”: [
“open”,
“close”
]
},
“orders”: {
“type”: “relation”,
“relation”: “oneToMany”,
“target”: “api::order.order”,
“mappedBy”: “batch”
},
“owner”: {
“type”: “relation”,
“relation”: “oneToOne”,
“target”: “plugin::users-permissions.user”
}
}
}

and when I try use api POST /api/batches with body request:
{
“data”: {
“name”: “Test Batch”,
“state”: “open”,
“locale”: “vi”,
“owner”: “rz9blkq8c0bns6bxx81heq9w”
}
}
}

it’s alway return error:
{
“data”: null,
“error”: {
“status”: 400,
“name”: “ValidationError”,
“message”: “Invalid key owner”,
“details”: {
“key”: “owner”,
“path”: “owner”,
“source”: “body”
}
}
}

How can I fix that? Thank you.

It looks like the problem is with how you’re sending the owner field. Since it’s a one-to-one relation, you need to send the user ID in this way:

{
“data”: {
“name”: “Test Batch”,
“state”: “open”,
“locale”: “vi”,
“owner”: {
“id”: “rz9blkq8c0bns6bxx81heq9w”
}
}
}