# How to handle a composite ID in a many-to-many?

**URL:** <https://discuss.jsonapi.org/t/how-to-handle-a-composite-id-in-a-many-to-many/187>\
**Category:** Uncategorized\
**Created:** [November 6, 2015, 6:07am UTC](https://discuss.jsonapi.org/t/how-to-handle-a-composite-id-in-a-many-to-many/187 "2015-11-06T06:07:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![collin](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/collin/32/111_2.png) [@collin](https://discuss.jsonapi.org/u/collin)\
**Post date:** [November 6, 2015, 6:07am UTC](https://discuss.jsonapi.org/t/how-to-handle-a-composite-id-in-a-many-to-many/187/1 "2015-11-06T06:07:05Z")

</div>

I am wondering how you represent a many-to-many relationship that is supported by a join table. Here is a  
[link](http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ "link") to a clear example. There is a `stock` table, `category` table, and a `stock_category` table to join them. A stock can have many categories and vice versa. So the join table has two ids, a `stock_id` foreign key to the stock table and a `category_id` foreign key to the category table. How is this represented as a relationship?

The other important part of this table structure is that the `stock_category` table also has additional attributes. It is its own resource in that sense. This part of it is similar to [these](http://discuss.jsonapi.org/t/pivot-data-for-many-to-many-relations/113) two\* questions. I suppose that since it is its own resource it isn’t many-to-many anymore. A `stock` has many `stock_category`, but a `stock_category` has only one `stock`.

Either way I am quite confused on the whole topic.

\*Edit, as a new user I can only put two links in a post. here is the 3rd: [Metadata About Relationships](http://discuss.jsonapi.org/t/metadata-about-relationships/86)

---

<div class="post-metadata">

**Author:** ![ethanresnick](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@ethanresnick](https://discuss.jsonapi.org/u/ethanresnick)\
**Post date:** [November 9, 2015, 2:05am UTC](https://discuss.jsonapi.org/t/how-to-handle-a-composite-id-in-a-many-to-many/187/2 "2015-11-09T02:05:29Z")

</div>

What (roughly) are the other attributes on your join table? Knowing the role of those attributes would give me a better sense of how you might best model this.

---

<div class="post-metadata">

**Author:** ![Ziege](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@Ziege](https://discuss.jsonapi.org/u/Ziege)\
**Post date:** [November 16, 2015, 7:05pm UTC](https://discuss.jsonapi.org/t/how-to-handle-a-composite-id-in-a-many-to-many/187/3 "2015-11-16T19:05:36Z")

</div>

You can solve this by adding an additional column to the reference table and set the primary key on this column (as well as a unique constraint for the other two). Perhaps you can also use a hidden id column for this (like OID in Postgres).

IMHO every table should have a one column primary, because it simplifies foreign key usage, working with IN lists, referencing, the later extension of the database structure etc. Many ORMs require this also.
