Set Relation using condition¶
API request replaces related objects with the ones identified in the API call. Child objects are referenced implicitly - through "whereClause" defining the condition for object selection.
Backendless.Data.of("TABLE-NAME").setRelation(parent, relationColumnName, whereClause): Promise<string>;
Backendless.Data.of(ClassFunctionRef).setRelation(parent, relationColumnName, whereClause): Promise<string>;
where:
Argument | Description |
---|---|
TABLE-NAME |
Name of the table where the parent object is stored. |
ClassFunctionRef |
Reference to a JS function/class identifying the table. Name of the table must match the name of the function. |
parent |
The object which will be assigned related children for relatedColumnName . This property expects the objectId value of the string or number type. When this argument is a plain JS object(for the "Untyped Objects" approach), it must contain the "objectId" property whose value must be a string or a number. Refer to the Data Import topic to learn more about the objectId value as the number. |
relationColumnName |
Name of the column identifying the relation. Objects from the childrenArray array will be set as related for the column in parentObject . The column name may optionally include table name separated by the colon character (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. |
Return Value¶
Number of child objects set into 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
argument:
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 name argument contains the child table qualifier, defined as ":Users:n"
right after the column name.
var parent = { objectId:"41230622-DC4D-204F-FF5A-F893A0324800" };
Backendless.Data.of( "Person" ).setRelation( parent,
"users:Users:n",
"name = \"Joe\" or name = \"Frank\"" )
.then( function( count ) {
console.log( "relation has been set" );
})
.catch( function( error ) {
console.log( "server reported an error - " + error.message );
});
function Person {
// define Person class properties here
}
var personObject = // personObject retrieval is out of scope in this example
Backendless.Data.of( Person ).setRelation( personObject,
"users:Users:n",
"name = \"Joe\" or name = \"Frank\"" )
.then( function( count ) {
console.log( "relation has been set" );
})
.catch( function( error ) {
console.log( "server reported an error - " + error.message );
});
Codeless Reference¶
The update operation presented further conditionally replaces the current 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 |
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
:
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:
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:
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.
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
: