Skip to content

Add Relation with objects

This API request adds related objects to the existing collection. Child objects to add to the relation must be explicitly defined by referencing their IDs.

Backendless.Data.of( "TABLE-NAME" ).addRelation( 
    parentObject,
    relationColumnName,
    childrenArray )
 .then( function( count ) {
  })
 .catch( function( error ) {
  });
Backendless.Persistence.of( DataTypeX ).addRelation( 
                                   parentObject,
                                   relationColumnName,
                                   childrenArray)
  .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 from the childrenArray array will be added 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
childrenArray an array of child objects to add to the relation identified by relatedColumnName. For the one-to-one relations the collection must contain one element. The array element can be either a complete object which must have the objectId property or a string value of the objectId property. Both of the following examples are valid values for the childrenArray argument:
[ "XXXX-XXXX-XXXX-XXXX", "ZZZZ-ZZZZ-ZZZZZ-ZZZZ" ]
[ {objectId:"XXXX-XXXX-XXXX-XXXX"}, {objectId:"ZZZZ-ZZZZ-ZZZZZ-ZZZZ" }]

Return Value

Number of child objects added to the relation.

Example

The example below adds a related object for a one-to-one column named address declared in the Person table. If the relation already contains an object, server returns an error since the addRelation API for one-to-one relations can add an object only once. 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 relationColumnName argument - notice the relation column name is "address". If it were specified as "address:Address:1", then the column would be created automatically.
relation-column

var parentObject = { objectId:"41230622-DC4D-204F-FF5A-F893A0324800" };

var childObject = { objectId:"3464C734-F5B8-09F1-FFD3-18647D12E700" };

var children = [ childObject ];

Backendless.Data.of( "Person" ).addRelation( parentObject, "address", children )
  .then( function( count ) {
    console.log( "relation has been set" );
  })
  .catch( function( error ) {
    console.log( "server reported an error - " + error.message );
  });
function Person() {
  // define Person properties here
}

function Address() {
  // define Address properties here
}

var personObject = // personObject retrieval is out of scope in this example
var addressObject = // addressObject retrieval is out of scope in this example

var addressCollection = [ addressObject ];

Backendless.Data.of( Person ).setRelation( personObject, "address", addressCollection )
  .then( function( count ) {
    console.log( "relation has been set" );
  })
  .catch( function( error ) {
    console.log( "server reported an error - " + error.message );
  });