Skip to content

Add Relation using condition

This API request adds related objects to the existing collection. Child objects to add to the relation are defined through a whereClause condition.

Backendless.Data.of( "TABLE-NAME" ).addRelation( 
       parentObject,
       relationColumnName,
       whereClause )
 .then( function( count ) {
  })
 .catch( function( error ) {
  });
Backendless.Data.of( DataTypeX ).addRelation( 
     parentObject,
     relationColumnName,
     whereClause )
  .then( function( count ) {
   })
  .catch( function( error ) {
   });

where:

Argument                Description
TABLE-NAME Name of the table where the parent object is stored.
DataTypeX Reference to a JS function/class identifying the table. Name of the table must match the name of the function.
parentObject The object which will receive related children for relatedColumnName. When this argument is a plain JS  object(for the "Untyped Objects" approach), it must contain the "objectId" property.
relationColumnName Name of the column identifying the relation. Objects identified by the whereClause argument will be added as related objects 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 added as related objects to the parent object.

Return Value

Number of child objects added to the relation.

Example

The following request adds objects from the Users table to the related property/column the Person object. Child objects added to the relation  must 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 added to 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  parentObject { objectId:"41230622-DC4D-204F-FF5A-F893A0324800" };

Backendless.Data.of( "Person" ).addRelation( parentObject, 
                                             "users:Users:n", 
                                             "name = \"Joe\" or name = \"Frank\"" )
 .then( function( count ) {
    console.log( "related objects have been added" );
  })
 .catch( function( error ) {
    console.log( "server reported an error - " + error.message );
  });
Person personObject = // personObject retrieval is out of scope in this example

Backendless.Data.of( Person.class ).addRelation( personObject, 
                                                 "users:Users:n", 
                                                 "name = \"Joe\" or name = \"Frank\"",
        new AsyncCallback<Integer>()
        {
          @Override
          public void handleResponse( Integer response )
          {
            Log.i( "MYAPP", "related objects have been added");
          }

          @Override
          public void handleFault( BackendlessFault fault )
          {
            Log.e( "MYAPP", "server reported an error - " + fault.getMessage() );
          }
        } );