RESTful URI是否应公开数据库主键?

时间:2022-07-07 19:35:48

I'll keep it simple. A Restful URI is something like:

我会保持简单。 Restful URI类似于:

example.com/rest/customer/1

What is the best common practice for what '1' is. Is it the db generated primary key?

什么是'1'是什么最好的常见做法。它是db生成的主键吗?

Using a system generated primary key makes me think that it won't be conducive to:

使用系统生成的主键让我觉得它不利于:

  1. Database merges
  2. Importing/exporting data

Not using the primary key has its own set of issues. Looking for prevailing thoughts on this topic.

不使用主键有自己的一组问题。寻找关于这个主题的流行思想。

3 个解决方案

#1


5  

I would expect the id to be the primary key as that is how you would identify the record. If you want, and you have one, you could use a natural primary key e.g. someone's employee id rather than an identity which is a surrogate key.

我希望id是主键,因为这是你如何识别记录。如果你愿意,你有一个,你可以使用自然的主键,例如某人的员工ID,而不是代理密钥的身份。

If your issue is that is is an integer rather than it being the database primary key (and hence I suppose guessable) you could use a GUID instead. They can be generated on either the client, or server side either in the application or in the DB.

如果您的问题是整数而不是数据库主键(因此我猜可以猜测),您可以使用GUID。它们可以在应用程序或数据库中的客户端或服务器端生成。

They would help with database merges etc. and they are guaranteed unique.

他们将帮助数据库合并等,并保证他们是独一无二的。

#2


1  

If you design your RESTful API correctly, the choice of numbering scheme for IDs becomes opaque and irrelevant to your API's consumers.

如果正确设计RESTful API,则ID的编号方案选择将变得不透明,与API的使用者无关。

Applications coded against your API will navigate around it using hyperlinks within your representations, as long as they don't attempt URI construction. Applying the principles of HATEOAS allows you to use database keys as your resource IDs without worry.

根据您的API编码的应用程序将使用您的表示中的超链接导航它,只要它们不尝试URI构造。应用HATEOAS的原则允许您使用数据库密钥作为资源ID而无需担心。

#3


0  

Generally, it doesn't matter.

一般来说,没关系。

But you should always be returning an href as your unique identifier to the consumer, not just an ID. If you just return an ID, that means they have to know where the resource lives and combine that information with the ID to make a unique request for the resource. Returning the href for them eliminates coupling.

但是你应该总是将一个href作为你的唯一标识符返回给消费者,而不仅仅是一个ID。如果您只返回一个ID,这意味着他们必须知道资源所在的位置,并将该信息与ID相结合,以便为资源提出唯一请求。返回它们的href消除了耦合。

#1


5  

I would expect the id to be the primary key as that is how you would identify the record. If you want, and you have one, you could use a natural primary key e.g. someone's employee id rather than an identity which is a surrogate key.

我希望id是主键,因为这是你如何识别记录。如果你愿意,你有一个,你可以使用自然的主键,例如某人的员工ID,而不是代理密钥的身份。

If your issue is that is is an integer rather than it being the database primary key (and hence I suppose guessable) you could use a GUID instead. They can be generated on either the client, or server side either in the application or in the DB.

如果您的问题是整数而不是数据库主键(因此我猜可以猜测),您可以使用GUID。它们可以在应用程序或数据库中的客户端或服务器端生成。

They would help with database merges etc. and they are guaranteed unique.

他们将帮助数据库合并等,并保证他们是独一无二的。

#2


1  

If you design your RESTful API correctly, the choice of numbering scheme for IDs becomes opaque and irrelevant to your API's consumers.

如果正确设计RESTful API,则ID的编号方案选择将变得不透明,与API的使用者无关。

Applications coded against your API will navigate around it using hyperlinks within your representations, as long as they don't attempt URI construction. Applying the principles of HATEOAS allows you to use database keys as your resource IDs without worry.

根据您的API编码的应用程序将使用您的表示中的超链接导航它,只要它们不尝试URI构造。应用HATEOAS的原则允许您使用数据库密钥作为资源ID而无需担心。

#3


0  

Generally, it doesn't matter.

一般来说,没关系。

But you should always be returning an href as your unique identifier to the consumer, not just an ID. If you just return an ID, that means they have to know where the resource lives and combine that information with the ID to make a unique request for the resource. Returning the href for them eliminates coupling.

但是你应该总是将一个href作为你的唯一标识符返回给消费者,而不仅仅是一个ID。如果您只返回一个ID,这意味着他们必须知道资源所在的位置,并将该信息与ID相结合,以便为资源提出唯一请求。返回它们的href消除了耦合。