Skip to content

Set/Add Relation using a condition

In this API request, child objects are referenced implicitly - through a whereClause defining the condition for object selection.

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>?whereClause=<whereClause> 

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.

Argument                Description
<whereClause> A where clause condition identifying objects in the child table which will be set as the related objects for the parent object.

Request Headers

user-token: value-of-the-user-token-header-from-login

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.

Request Body

None

Return Value

Error or number of objects the operation sets for the relation.

Example

The following request creates a relation between a Person object and all objects in the Users table which match the provided query. The query is specified in the whereClause parameter in the URL. The value of the whereClause parameter is URL-encoded. The decoded version of the query is: name='Joe' or name = 'Frank'. As a result of the operation, all User objects where the name property is either Joe or Frank will be set in the relation.

The relation column is created if it does not exist. This is done because the column contains the child table qualifier, defined as ":Users:n" right after the column name.

Important

Make sure to replace xxxx in the domain name in the sample request below to the one assigned to your application.

curl \  
-X POST  
https://xxxx.backendless.app/api/data/Person/parentObjectId/friends:Users:n?whereClause==name%3D%27Joe%27%20or%20name%20%3D%20%27Frank%27

Codeless Reference

Set Relations Using a Condition

The update operation presented further conditionally replaces the current relations with the new ones.

data_set_add_relation_condition_example_7

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 You must use the where clause condition in this property to select specific children objects from the data table. For more information about where clause conditions, refer to the Search with the Where Clause topic.

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:
data_set_add_relation_object_example_1

By clicking the record (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:

data_set_relation_condition_example_2

Suppose, you want to set a new relation between the first object(parent) in the employees data table and the third object(having value C++) in the uniqueSkills data table presented below:

data_set_add_relation_object_example_2

The example below sets a new relation between the parent object and the child object stored in the related data table. The object selection for the relation is made using the where clause condition in the children property, which is set to "skill = 'C++'". This will instruct the operation to search the related data table for an object that has the 'C++' value in the skill column. If the object with this value is found, then a new relation gets established.

data_set_relation_condition_example_3

As you can see, this operation has set the new relation between the parent object and the child object from the related data table uniqueSkills:

data_set_relation_condition_example_4

Add Relations Using a Condition

data_set_add_relation_condition_example_3

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 You must use the where clause condition in this property to select specific children objects from the data table. For more information about where clause conditions, refer to the Search with the Where Clause topic.

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.

data_set_add_relation_object_example_1

The child data table uniqueSkills contains the following records:

data_set_add_relation_object_example_2

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.

data_set_add_relation_object_example_3

Suppose you need to add a new relation between the parent object (name: Alex``Lincoln, objectId: 2A378AC8-0F70-423D-8C38-107A2B2F0C1E) and the child object with the value "C++" stored in the related data table.

The example below utilizes the where clause expression "skill = 'C++'" specified in the children property which instructs the operation to search the uniqueSkills data table for an object containing the 'C++' value in the "skill" column and establish a new relation with the parent object.

data_add_relation_condition_example_3

After the Codeless logic, the new relation gets established between the child object and the parent object stored in the employees data table:

data_add_relation_condition_example_4

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:

data_set_add_relation_object_example_6

Consider the records in the second data table called uniqueSkills:

data_set_add_relation_object_example_2

Imagine you want to establish relations with all objects stored in the uniqueSkills data table except for objects that have values "C++" and "REST" in the skill column.

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 three children objects from the "uniqueSkills" data table using the where clause condition expressed as "skill != 'C++' and skill != 'REST'" in the children property:

  1. (skill: Javascript, objectId: 4CCE1B29-70C6-4405-9614-6A17AFB159F7)

  2. (skill: Java, objectId: 9FD683B9-46A4-4AF2-8B6D-D5417AEA670E)

  3. (skill: Objective-C, objectId: F6A3B4A2-B189-4D42-A681-027251C3DCCB)

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.

data_add_relation_condition_example_1

After the Codeless logic runs, the new relation column "skills" gets created in the "employees" data table.

data_set_add_relation_object_example_8

And three new relations are added between the children objects and the parent object stored in the employees data table:

data_add_relation_condition_example_2