# Optional relationship?

**URL:** <https://discuss.emberjs.com/t/optional-relationship/19788>\
**Category:** Ember Data\
**Created:** [November 10, 2022, 10:22pm UTC](https://discuss.emberjs.com/t/optional-relationship/19788 "2022-11-10T22:22:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bayleaf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bayleaf/32/18444_2.png) [@bayleaf](https://discuss.emberjs.com/u/bayleaf)\
**Post date:** [November 10, 2022, 10:22pm UTC](https://discuss.emberjs.com/t/optional-relationship/19788/1 "2022-11-10T22:22:58Z")

</div>

I’m using Ember Data 4.1 and Ember 4.7.

I have tables named person and photo. What I want is for this relationship to be optional. A person may have many photos or none at all. A photo may be owned by a person or have no owner.

I thought I could use hasMany and belongsTo in both model scripts by setting {inverse: null} like:

My person model has this: @hasMany(“photo”, { inverse: null }) photo;

My photo model has this: @belongsTo(“person”, { inverse: null }) person\_id;

This works fine until I try to save a photo with the photo.person\_id set to null. But while the photo record is saved with photo.person\_id = null, Ember Data tries to find a person with an id of 0. That results in this error:

Error: Assertion Failed: You made a ‘findRecord’ request for a ‘person’ with id ‘0’, but the adapter’s response did not have any data.

My API returns false when a single record is not found. Is there a way to do what I’m trying to do with Ember Data?

---

<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:** [November 10, 2022, 11:11pm UTC](https://discuss.emberjs.com/t/optional-relationship/19788/2 "2022-11-10T23:11:48Z")

</div>

I think Ember Data works how you expect, relationships are optional. The problem is that it think there’s a record with id 0 and it’s trying to fetch it. This makes it sound like the problem _might_ actually be with serialization or with the API responses. Can you share more about your API responses for the POST requests?

---

<div class="post-metadata">

**Author:** ![bayleaf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bayleaf/32/18444_2.png) [@bayleaf](https://discuss.emberjs.com/u/bayleaf)\
**Post date:** [November 11, 2022, 12:06am UTC](https://discuss.emberjs.com/t/optional-relationship/19788/3 "2022-11-11T00:06:09Z")

</div>

The Request payload, when I set person\_id =null, includes ` person_id: null` and my backend also records it as null .

However, what I didn’t notice before is that the response is different and includes:` "person_id": 0`. I guess that is why Ember Data is trying to locate a person with id=0.

How do I fix 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:** [November 11, 2022, 12:18am UTC](https://discuss.emberjs.com/t/optional-relationship/19788/4 "2022-11-11T00:18:11Z")

</div>

Ok that’s kinda what I was guessing. Do you have control over the API code? Sounds like maybe the backend is serializing null =\> 0 for some reason.

If not you could also handle this in the ember serializer layer by taking a relationship id value of `0` and replacing it with `null`. Several ways to accomplish that, `normalize()` is generally a good place to start.

---

<div class="post-metadata">

**Author:** ![bayleaf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bayleaf/32/18444_2.png) [@bayleaf](https://discuss.emberjs.com/u/bayleaf)\
**Post date:** [November 11, 2022, 3:27pm UTC](https://discuss.emberjs.com/t/optional-relationship/19788/5 "2022-11-11T15:27:28Z")

</div>

Your guess was dead on – my backend was serializing null values to 0. Once I fixed that, everything works fine. Thanks for your help!

---

<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:** [November 11, 2022, 3:35pm UTC](https://discuss.emberjs.com/t/optional-relationship/19788/6 "2022-11-11T15:35:04Z")

</div>

Awesome! Glad you got it working!
