So I have some resources in my API with foreign key relationships and these fk records (by design) cannot be created unless a POST is made to that specific resource first.
I cannot create a Country by POSTing to Wine even though Country is a foreign key.
the POST is going to /api/wine not /api/country
In other words, if I send this payload to Wine resource:
{
id: 79,
name: "Bordeaux",
country: {
id: 76,
code: "FR",
name: "France"
},
year: "2005"
}
It will fail unless that country already exists. We cannot create the country from this POST because we are POSTing to the Wine resource and not Country.
This was by design because there are other foreign key relationships in the API that the consumer should never be able to modify or create. Furthermore, letting the consumer create multiple resources from a single payload seems like it would open up the possibility of introducing typo's, duplicate records, and dirtying the data, especially as the API scales and people are writing scripts around it and getting familiar with pushing data into it.
If I have a GET endpoint that reflects a chain of foreign key relationships
/api/planet/country/state/city/postal_code/
and I make a POST to:
/api/postal_code/
I should not be able to create a planet, country, state or city, I should only be able to refer to existing records for these resources if they exist and then finally create a new postal code.
My question is simply, what is the standard practice for this? Which methodology is more commonly used. If I were to open up my API to support creating every foreign key resource from a single endpoint - it seems like it would remove some of the benefits of using foreign key relationships in the first place.