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:
Codeless Reference¶
Set Relations With Objects¶
The update operation presented further replaces existing objects relations with the new ones.
where:
Argument | Description |
---|---|
table name |
Name of the table where which contains the parent object as identified by parent object . |
parent object |
Id of the object for which the relation will be created/set. |
relation name |
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):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. |
children |
A list containing unique identifiers(objectId ) of the children objects that are used to update existing relations of the parent object . The objectId can be a string value or a number. |
Returns the number of newly set object relations.
The operation requires two data tables to update existing relations. Consider the first object with one-to-many relations(skills
column) in the parent data table called employees
:
By clicking the value (1:N Relations
) in the skills
column of the parent data table presented above, you get redirected to the child data table called uniqueSkills
, where you can see the related children objects:
Suppose, you want to set new relations between the parent object in the employees
data table and the third/fifth objects(having values REST
and C++
) in the uniqueSkills
data table presented below:
The example below updates the current relations for the parent object
. Children objects that are used to update existing relations:
-
(
skill: REST, objectId: DA9EC928-4B1A-4F57-8C2E-4402D42FE8C9
) -
(
skill: C++, objectId: EA8D494F-50D4-445B-8F4E-F6EBC4C39D9F
)
As you can see, this operation has replaced the old relations(objects having Javascript
and Java
values) with the new ones(objects having C++
and REST
values):
Add Relations With Objects¶
where:
Argument | Description |
---|---|
table name |
Name of the table where which contains the parent object as identified by parent object . |
parent object |
Id of the object for which the relation will be created/set. |
relation name |
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):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. |
children |
A list containing unique identifiers(objectId ) of the children objects that are used to create a relation with the parent object . String value or number. |
Returns the number of added object relations.
Adding New Relations
Consider the records stored in the following parent data table called employees
. As you can see, this data table contains the skills
column that has relation to the child data table called uniqueSkills
.
The child data table uniqueSkills
contains the following records:
By clicking the value (1:N Relations
) in the skills
column of the parent data table employees
, the system displays currently related objects, which in this case are Java
and Javascript
.
Suppose you need to add new relations to the parent object. The example below creates one-to-many relation between the parent object (name
: Alex``Lincoln
, objectId
: 2A378AC8-0F70-423D-8C38-107A2B2F0C1E
) and two children objects from the uniqueSkills
data table:
-
(
skill: C++, objectId: DA9EC928-4B1A-4F57-8C2E-4402D42FE8C9
) -
(
skill: REST, objectId: EA8D494F-50D4-445B-8F4E-F6EBC4C39D9F
)
The relation name
property references the name(skills
) of the related column in the parent data table. Related objects must be referenced in the children property as the list containing "objectId"
values of the children objects.
After the Codeless logic, two new relations are added between the children objects and the parent object stored in the employees
data table:
Creating Relation Column and Adding New Relations
Suppose you have two data tables with no established relations and you need to create a new relation column and add new object relations to the parent object in one request.
Consider the records in the first data table called employees
. As you can see, there is no relation column yet:
Consider the records in the second data table called uniqueSkills
. The first two objects(having values Javascript
and Java
) stored in this data table must establish relations with the parent object stored in the employees
data table:
The example below creates a new column called "skills"
in the "employees"
data table and establishes one-to-many relations between the parent object (name: Alex Lincoln, objectId: 2A378AC8-0F70-423D-8C38-107A2B2F0C1E
) and two children objects from the "uniqueSkills"
data table:
-
(
skill: Javascript, objectId: 4CCE1B29-70C6-4405-9614-6A17AFB159F7
) -
(
skill: Java, objectId: 9FD683B9-46A4-4AF2-8B6D-D5417AEA670E
)
For the operation to work in this way, you must specify the expression "skills:uniqueSkills:n"
in the relation name
column. Where "skills"
is the name of the relation column to create in the parent data table called "employees"
, "uniqueSkills"
is the name of the child data table where objects are stored, and the ":n"
represents the relations cardinality which is one-to-many.
After the Codeless logic runs, the new relation column "skills"
gets created in the "employees"
data table.
And two new relations are added between the children objects and the parent object stored in the employees
data table: