Post data via REST API without ID as relation but some another field

System Information
  • Strapi Version: v3.5.2
  • Operating System: Manjaro Linux 5.10.19-1-MANJARO
  • Database: MySQL (MariaDB)
  • Node Version: v14.16.0
  • NPM Version: 7.6.2
  • Yarn Version: 1.22.10

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.

1 Like

i have same issue. using id is bad because what if ids change then all data will be wrong

Same issue here :wave: :wave:
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…

It’s a little bit hard to explain

Same issue here :wave:

Now is 2024, no solution yet?

1 Like

Same exact issue, any updates?

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