Set/Add Relation with objects¶
API request must identify the child objects to set/add to the relation explicitly by referencing their IDs.
Methods¶
Set relation - POST
Add relation - PUT
Endpoint URL¶
The xxxx.backendless.app
is a subdomain assigned to your application. For more information see the Client-side Setup section of this documentation.
https://xxxx.backendless.app/api/data/<table-name>/<parentObjectId>/<relationName>
where:
Argument | Description |
---|---|
<table-name> |
Name of the table where which contains the parent object as identified by <parentObjectId> . |
<parentObjectId> |
Id of the object for which the relation will be created/set. |
<relationName> |
Name of the column which identifies the relation within the parent table (identified as <table-name> ). The column name may optionally include table name separated by the colon character as well as cardinality which determines the type of relationship (one to one or one to many) (see the note below): |
Important
If the column does not exist in the parent table at the time when the API is called, the value of the "relationColumnName
" argument must include the name of the child table separated by colon and the cardinality notation. The cardinality is expressed as ":1
" for one-to-one relations and ":n
" for one-to-many relations. For example, the value of "myOrder:Order:1
" will create a one-to-one relation column "myOrder
" in the parent table. The column will point to the Order
child table. Likewise, the value of "myOrder:Order:n
" will create a one-to-many relation column "myOrder
" pointing to the Order
table.
Request Headers¶
user-token: value-of-the-user-token-header-from-login
Content-Type:application/json
where:
Argument | Description |
---|---|
user-token |
Optional header. Contains a value returned by Backendless in a preceding user Login API call. If user-token is set in the request, the operation will be executed with the security policy associated with the currently logged in user. This means all permissions associated with the user and the roles assigned to the user will be enforced by Backendless. |
Content-Type |
Must be set to application/json . This header is mandatory. |
Request Body¶
Must be an array of child object IDs as string values.
[
"childObjectId1", "childObjectId2"
]
Return Value¶
Error or number of objects the operation sets for the relation.
Example¶
The example below sets a relation for a one-to-one column named address
declared in the Person
table. The column must be declared in the table prior to the execution of the request shown below. This necessity is explained by missing table name qualifier in the URL - notice the relation column is address
. If it were specified as address:Address:1
, then the column would be created automatically.
curl request:
Important
Make sure to replace xxxx in the domain name in the sample request below to the one assigned to your application.
curl \
-H Content-Type:application/json \
-X POST
-d "[ \"XXXXX-XXXXX-XXXXX-XXXXX\" ]" \
https://xxxx.backendless.app/api/data/Person/parentObjectId/address
The following example sets a relation for a one-to-many column named friends
which will be created if missing in the Person
table. The column points to the Users table:
curl \
-H Content-Type:application/json \
-X POST
-d "[ \"XXXXX-XXXXX-XXXXX-XXXXX\" ]" \
https://xxxx.backendless.app/api/data/Person/parentObjectId/friends:Users:n
Since the path includes the name of the child table (see the part of the path which included friends:Users:n
), Backendless will create the column if it does not exist. Once created, you can see the column in Backendless console: