# Create/update relationships with attributes

**URL:** <https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701>\
**Category:** Uncategorized\
**Created:** [September 24, 2019, 6:47pm UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701 "2019-09-24T18:47:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saras](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@saras](https://discuss.jsonapi.org/u/saras)\
**Post date:** [September 24, 2019, 6:47pm UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701/1 "2019-09-24T18:47:11Z")

</div>

Hello,

after reading specification, I’m a bit confused about creating/updating resources with relationships that contains attributes. Let’s say I need to create new resource with relationship that has attributes, I’d do it like this:

```
{
    "data": {
        "type": "carts",
        "attributes": {
            note: "Some note about cart",
         },
         "relationships": {
             "products": [
                  {
                      "data": {  
                          "type": "products",
                          "id": "10",
                          "attributes": {
                              "quantity": 5
                          }
                      }
                  }
              ]
         }
    }    
}

```

Althouth I could not find this stated explicitly (maybe I’ve missed it), but for me it seems they way to do this. Am I correct about it?

Thanks in advance for your replies! 🙂

---

<div class="post-metadata">

**Author:** ![jelhan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jelhan/32/822_2.png) [@jelhan](https://discuss.jsonapi.org/u/jelhan)\
**Post date:** [September 25, 2019, 6:09pm UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701/2 "2019-09-25T18:09:26Z")

</div>

I get what you are trying to do but unfortunately that’s not supported by spec.

The `data` value of a relationship object must only be a resource identifier objects (has-one) or an array of resource identifier objects (has-many). Using a resource object is not supported. These ones may only be linked by the resource identifier objects. Therefore this feature is called resource linkage.

I get from your example that you are trying to put additional information on the relationship itself. You could model that as meta information using a meta object but I would not recommend this. This goes against the intend of the specification and updating a meta information is not covered by spec.

I would recommend to add an intermediate model, e.g. `cart-items` which has a relationship to `carts` and `products` and a `quantity` attribute.

Unfortunately JSON:API spec does not support creation of multiple resources with one request yet. So you would need to create the cart and it’s `cart-items` with separate requests. This limitation should be solved by a new feature called [operations](https://github.com/json-api/json-api/pull/1254), which is proposed for v1.2.

---

<div class="post-metadata">

**Author:** ![saras](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@saras](https://discuss.jsonapi.org/u/saras)\
**Post date:** [September 27, 2019, 7:29am UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701/3 "2019-09-27T07:29:19Z")

</div>

@jelhan thanks a lot for your reply! 🙂

---

<div class="post-metadata">

**Author:** ![rafael](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@rafael](https://discuss.jsonapi.org/u/rafael)\
**Post date:** [December 21, 2024, 12:21am UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701/4 "2024-12-21T00:21:29Z")

</div>

I believe there’s nothing forbidding the client to use the `included` top-level member. Would you agree the following is valid?

```auto
{
    "data": {
        "type": "carts",
        "attributes": {
            "note": "Some note about cart",
        },
        "relationships": {
            "products": [
                "data": {
                    "type": "products",
                    "lid": "some-uuid"
                }
            ]
        }
    },
    "included": [
        {
            "type": "products",
            "lid": "some-uuid",
            "attributes": {
                "quantity": 5
            }
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![jelhan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jelhan/32/822_2.png) [@jelhan](https://discuss.jsonapi.org/u/jelhan)\
**Post date:** [May 9, 2025, 10:14am UTC](https://discuss.jsonapi.org/t/create-update-relationships-with-attributes/1701/5 "2025-05-09T10:14:35Z")

</div>

Compound documents are only valid for responses. Creating, modifing, or deleting multiple resources in a single request is not supported by the base specification. However it can be achieved using an extension. The [Atomic Operations](https://jsonapi.org/ext/atomic/) and [Bulk Create](https://github.com/jelhan/json-api-bulk-create-extension) extensions are two examples how that could be done.
