# Best practices with many to many relationships in JsonAPI

**URL:** <https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982>\
**Category:** Ember Data\
**Created:** [March 1, 2016, 9:59am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982 "2016-03-01T09:59:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/henry/32/16850_2.png) [@Henry](https://discuss.emberjs.com/u/Henry)\
**Post date:** [March 1, 2016, 9:59am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/1 "2016-03-01T09:59:50Z")

</div>

Hi guys,

how do you let the server know that there is a relationship `many-to-many` between two models when storing them? I know that in `ember-data` a relationship of this kind can be easily implemented by using `hasMany` in both models but when you want to save them, how does the server know that there’s a relationship between them? because in the models there’s no info about their relationship (apart from the `hasMany` property). Do you customize the serializer to send both models together? do you add info in the `meta` to let the server know that it has to do something else with that record?

---

<div class="post-metadata">

**Author:** ![teejay](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/teejay/32/13585_2.png) [@teejay](https://discuss.emberjs.com/u/teejay)\
**Post date:** [December 13, 2018, 4:01am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/2 "2018-12-13T04:01:08Z")

</div>

This is a question I really would like to have answered too. I am also quite uncertain what json:api input I have to give to work with embers many-to-many implementation. How does the json need to look like ? There is very little documentation in the jsonapi spec so I am unsure how this works.

---

<div class="post-metadata">

**Author:** ![wukongrita](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/wukongrita/32/16645_2.png) [@wukongrita](https://discuss.emberjs.com/u/wukongrita)\
**Post date:** [January 29, 2020, 7:59am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/3 "2020-01-29T07:59:14Z")

</div>

@Henry, @teejay Any update on how to solve this?

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [January 29, 2020, 3:35pm UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/4 "2020-01-29T15:35:51Z")

</div>

Do you have any more specific questions?

Broad strokes:

- relationships in ember-data have automatically inferred inverses, but I prefer to specify them explicitly, anyway because of the inverses you should only have to set the relationship one way and it will link them
- both records should be saved, meaning a request is made (POST/PUT/PATCH depending on your scenario and config) for each of the records
- usually (and this should be done automatically by the JSON API serializer) the relationship is serialized with the type and ID of the related records. .

If you’re running into issues or have specific questions feel free to ask!

---

<div class="post-metadata">

**Author:** ![hskartono](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/hskartono/32/17048_2.png) [@hskartono](https://discuss.emberjs.com/u/hskartono)\
**Post date:** [May 14, 2020, 4:01am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/5 "2020-05-14T04:01:13Z")

</div>

Is it possible to add transaction to make those action save? For example, if one of the record failed to save, do we have any way to abort previously saved record?

---

<div class="post-metadata">

**Author:** ![Henry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/henry/32/16850_2.png) [@Henry](https://discuss.emberjs.com/u/Henry)\
**Post date:** [June 2, 2020, 8:41am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/6 "2020-06-02T08:41:43Z")

</div>

One alternative is to extend `ember store` to save model domains, so you send the main model and its relationships together in a single request.

---

<div class="post-metadata">

**Author:** ![hskartono](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/hskartono/32/17048_2.png) [@hskartono](https://discuss.emberjs.com/u/hskartono)\
**Post date:** [June 2, 2020, 11:41am UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/7 "2020-06-02T11:41:46Z")

</div>

@Henry do you have any example code for this?

---

<div class="post-metadata">

**Author:** ![Henry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/henry/32/16850_2.png) [@Henry](https://discuss.emberjs.com/u/Henry)\
**Post date:** [June 2, 2020, 12:02pm UTC](https://discuss.emberjs.com/t/best-practices-with-many-to-many-relationships-in-jsonapi/9982/8 "2020-06-02T12:02:29Z")

</div>

[Here](https://github.com/Netflix/ember-batch-request/blob/master/app/services/store.js) you have an example of the `store` service being extended to support batch operations.
