Hi. I would like to make a POST request via REST API and provide a relation. But in my case, I cannot provide the primary key of relation which is “id” generated by Strapi. I want to use as relation another field added by me which is “code” (type of string).
Should I make a custom endpoint in that case or it’s possible somehow?
I want to POST a request with data:
{
"name": "nowosolski",
"description": "",
"code": "04",
"voivodeship": { "code": "08" } // here should be ID
}
So instead of ID in “voivodeship”, I would like to use an additional field. Is it possible to do?
Or I should provide a new endpoint to search by “code” field then after I get it make another request with POST.
Same issue here
The only solution that i found possible was creating a method to convert in some way the field that you want to the id and send the id instead the field that you set…
After going through the hunt, I got tired and did a workaround.
You can instead make a GET request to every field in the “languages” collection and get the id and name associated with the fields like this:
let mappedLanguages = ;
for(i = 0; i < collection.length; i++) {
API call
mappedLanguages.push({id, name})
}
And now when you want to relate, instead of id, use the mappedLanguages array and find the id associated with the name you want to use, like this:
data: {
languages: mappedLanguages.find(lng => lng.name == “<Your_string>”)?.id
}
This works for me, I’m solving a problem by creating a problem but hey, it’s better than spending hours to find a way, how to refer another field instead of ids