Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good/bad design to join composite key with underscore in url?

I'm looking for the best practices on RESTful API design for the following use case:

Table1     Table2
Id1        Id1
Id2        Id2
Id3        Id3
Name       Name
           Table1Id1(FK to Table1)
           Table1Id1(FK to Table1)
           Table1Id1(FK to Table1)

Suppose i have endpoints like below for Table1:

/root/table1 (to get list of records)
/root/table2 (to get single record by primary key)

Now here my question is which would be the best way from below two to represent composite key in second url :

/root/e1/Id1/Id2/Id3

or 

/root/e1?Id1=1&Id2=2&Id3=3

Suppose i have endpoints like below for Table2:

/root/table1/Table1Id1_Table1Id2_Table1Id1/table2 (to get list of records for table2 by table1).

Now here is my question that is above url valid and appropriate in case of composite key?

Any advice on a good pattern to follow for this use case would be greatly appreciated.

like image 957
ILoveStackoverflow Avatar asked Aug 05 '17 14:08

ILoveStackoverflow


People also ask

Is it good to have composite primary key?

Composite keys in SQL prove to be useful in those cases where you have a requirement of keys that can uniquely identify records for better search purposes, but you do not possess any single unique column. In such cases, you must combine multiple columns to create a unique key.

Is that true join can be done on composite keys?

It is technically possible to join these tables -- you can join on any columns regardless of whether they are key columns or not.

Why would you need to use a synthetic or composite key?

A synthetic primary key says nothing about the entity you are modeling, it's merely a number. A composite key is better since it is tied to the domain.


2 Answers

Any advice on a good pattern to follow for this use case would be greatly appreciated.

Don't couple your resource identifiers to your (current) database schema; that violates encapsulation.

I'm looking for the best practices on RESTful API design for the following use case

REST really doesn't care. As far as REST is concerned, the URI is opaque; any information encoded into it is done at the server's discretion and for its own use.

The relevant concerns are RFC 3986, and your local design conventions.

The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component (Section 3.4), serves to identify a resource within the scope of the URI's scheme and naming authority (if any).

Path elements are supposed to be for hierarchical data -- think about the way that relative URIs resolve.

Based on your description here, I wouldn't think that the foreign keys have a natural hierarchy to them; certainly not in the general case. So using the non hierarchical part of the URI (the query) might make more sense.

Another possibility to consider would be matrix parameters; you can combine the foreign keys into a single path segment, thereby avoiding any suggestion of hierarchy among them.

like image 114
VoiceOfUnreason Avatar answered Oct 13 '22 13:10

VoiceOfUnreason


Agree with VoiceOfUnreason

Don't couple your resource identifiers to your (current) database schema; that violates encapsulation.

Dont do that

For your specific usecase

As a general best practice, REST Urls should follow a predictable and hierarchical pattern to identofy resources. This brings much transparency between Client and Server. So suppose you have parent-child relations ship in your entity it should be better be designed as

/{appname}/{version}/parent/{parentId}/child/{childId}

instead of just having

/{appname}/{version}/child/{childId}

In your usecase the non-hierarchical part of URL is Id1/Id2/Id3

Best approach to have a unique identifier for your tables. But if it really can not be done then you should rather go for

/root/e1?Id1=1&Id2=2&Id3=3

This gives a notion "get me the e1 with is having Id "1" and Id "2" and Id "3" which is correct in your context

/root/e1/Id1/Id2/Id3

This is non standard and therefore should be avoided

to get list of records for table2 by table1

Your should have

/root/table1/table2?Table1Id1&Table1Id2&Table1Id1
like image 24
Abhijit Mazumder Avatar answered Oct 13 '22 12:10

Abhijit Mazumder