Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should RESTful URIs expose database primary keys?

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

example.com/rest/customer/1

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

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.

like image 344
rouble Avatar asked Jan 12 '23 12:01

rouble


1 Answers

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.

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.

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

like image 117
GraemeMiller Avatar answered Jan 19 '23 03:01

GraemeMiller