从另一个资源POST请求创建外键记录是否为RESTful? [英] Is it RESTful to create a foreign key record from another resources POST request?

查看:98
本文介绍了从另一个资源POST请求创建外键记录是否为RESTful?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的API中有一些具有外键关系的资源,并且这些fk记录(通过设计)无法创建,除非首先对该特定资源进行POST.

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.

即使Country是外键,我也无法通过发布到Wine来创建Country.

I cannot create a Country by POSTing to Wine even though Country is a foreign key.

POST将转到/api/wine而不是/api/country

the POST is going to /api/wine not /api/country

换句话说,如果我将此有效载荷发送到Wine资源:

In other words, if I send this payload to Wine resource:

{
  id: 79,
  name: "Bordeaux",
  country: {
      id: 76,
      code: "FR",
      name: "France"
    },
  year: "2005"
}

除非该国家已经存在,否则它将失败.我们无法通过此POST创建国家/地区,因为我们正在发布到Wine资源而不是Country.

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.

这是设计使然,因为API中还有其他外键关系,使用者永远都不能修改或创建.此外,让消费者从单个有效载荷创建多个资源似乎将打开引入错别字,重复记录并弄脏数据的可能性,尤其是当API扩展并且人们正在围绕它编写脚本并熟悉推送数据时进入它.

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.

如果我的GET端点反映了一系列外键关系

If I have a GET endpoint that reflects a chain of foreign key relationships

/api/planet/country/state/city/postal_code/

我将发布到:

/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.

我的问题很简单,这是什么标准做法?哪种方法更常用.如果我要开放我的API以支持从单个端点创建每个外键资源-似乎它将消除首先使用外键关系的一些好处.

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.

推荐答案

在调用新的create child-resource api时创建父资源不是RESTful的.如果必须存在父资源才能创建子资源,则最好的方法是遵循分层api结构,如果找不到所引用的父资源,则抛出异常.

Creating a parent resource when a new create child-resource api is called is not RESTful. If the parent-resource has to exist in order to create a child resource, the best way is to follow the hierarchical api structure and throw exceptions if the referred parent resource is not found.

Hierarchical API structure :
/api/countries/$country_id/wines - POST

如果遵循此结构,则api使用者将知道必须存在这些父资源,客户端api才能正常工作.

If you follow this structure the api consumers will know that these parent resources must exist for the client apis to work properly.

另一方面,如果您觉得api变得更长,则可以在父资源(国家/地区)不存在时抛出异常.

On the other hand, if you feel that the api becomes lengthier, you can just throw exceptions when the parent resource(country) does not exist.

这篇关于从另一个资源POST请求创建外键记录是否为RESTful?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆