# Composite ID inside the resource object

**URL:** <https://discuss.jsonapi.org/t/composite-id-inside-the-resource-object/367>\
**Category:** Uncategorized\
**Created:** [February 29, 2016, 2:20pm UTC](https://discuss.jsonapi.org/t/composite-id-inside-the-resource-object/367 "2016-02-29T14:20:21Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![ylazaryev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/ylazaryev/32/257_2.png) [@ylazaryev](https://discuss.jsonapi.org/u/ylazaryev)\
**Post date:** [April 5, 2016, 3:15pm UTC](https://discuss.jsonapi.org/t/composite-id-inside-the-resource-object/367/3 "2016-04-05T15:15:33Z")

</div>

@brainwipe first of all thank you for the suggestion to model the problem with relations.  
However, this is not an answer to the original problem (the question wasn’t properly defined, sorry)

Let me clarify it:

- Some entities have a natural unique key ([https://en.wikipedia.org/wiki/Natural\_key](https://en.wikipedia.org/wiki/Natural_key)) coming from business  
_Example: consider Product that is defined by EAN (European Article Number). In this case EAN is used as “id” attribute as it uniquely identifies a product (at least in Europe)._

- Other entities don’t have a unique key that comes from business, but they do have a surrogate unique key ([https://en.wikipedia.org/wiki/Surrogate\_key](https://en.wikipedia.org/wiki/Surrogate_key)) that is generated by db (Using SERIAL type in Postres, for example). In this case its also can be used in “id” attribute.

- And there are entities that have [compound keys](https://en.wikipedia.org/wiki/Compound_key) and no surrogate keys. **The problem is that they don’t fit into string-typed “id” attribute**

In your proposal above there is an assumption that both entities “a” and “b” have scalar keys (either natural or surrogate).

Please consider this somewhat contrived example:  
We are modeling and inventory microservice that is capable of saving quantities of products per merchant. Some other microservices are responsible for **Merchant** and **Product** entities; So there is a **Quantity** entity our Inventory microservice is responsible for.

- Say **Merchant** entity has a surrogate unique key (scalar, generated by database)
- **Product** entity has a natural unique key - EAN ([https://en.wikipedia.org/wiki/International\_Article\_Number\_(EAN)](https://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29))
- but **Quantity** is an [associative entity](https://en.wikipedia.org/wiki/Associative_entity) which is identified by pair of keys (merchant-id + EAN) only (because its set per merchant per product)

And its not clear how to expose **Quantity** using JSON-API compliant API because its not clear what to put in its “id” attribute.

---

_[View the full topic](https://discuss.jsonapi.org/t/composite-id-inside-the-resource-object/367)._
