Skip to content

Update with Adding a Relation

The Deep Save API can be used to update an existing object and set a relation with a new child (or parent). Consider the examples below:

Existing Parent and New Child

This example retrieves an existing parent object (Order) and uses the Deep Save API to perform the following:

  1. Save new child object (OrderItem)
  2. Establish relationship between the Order and the new OrderItem.

The Order object already has two related OrderItem objects before the sample code below runs:

order-with-relations

Important

When working with relationships, the Deep Save API applies the "accumulation" policy. This means the following: if a parent object in the database already has some related objects and you update the object with new child objects in the relation, these objects will be added to the relation.

curl \  
-H Content-Type:application/json \  
-X PUT  \  
-d "{  
  \"objectId\":\"479090D2-5E85-48AB-B959-3FA14859A222\",  
  \"orderItems\":[  
    {  
      \"name\":\"Grapes\",  
      \"price\":3.99  
    }  
  ]  
}" \  
-v https://xxxx.backendless.app/api/data/Product/deep-save

Server response:

{
  "___class": "Order",
  "ownerId": null,
  "orderItems": [
    {
      "created": 1624093599000,
      "price": 3.99,
      "name": "Grapes",
      "___class": "OrderItem",
      "ownerId": null,
      "updated": null,
      "objectId": "BB0A82BA-D108-406C-A898-3064E566E6F2"
    }
  ],
  "updated": null,
  "objectId": "479090D2-5E85-48AB-B959-3FA14859A222",
  "created": 1624093011000
}

Codeless reference:

update-order-new-orderitem

After the code runs, the new OrderItem object is saved in the database and the Order object relation is expanded to include it:

order-with-relations-after-deepsave