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.

- (void)addRelationWithColumnName:(NSString * _Nonnull)columnName parentObjectId:(NSString * _Nonnull)parentObjectId childrenObjectIds:(NSArray<NSString *> * _Nonnull)childrenObjectIds responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func addRelation(columnName: String, parentObjectId: String, childrenObjectIds: [String], responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

- (void)addRelationWithColumnName:(NSString * _Nonnull)columnName parentObjectId:(NSString * _Nonnull)parentObjectId childrenObjectIds:(NSArray<NSString *> * _Nonnull)childrenObjectIds responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func addRelation(columnName: String, parentObjectId: String, childrenObjectIds: [String], responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
parentObjectId must be objectId of the object which will be assigned related children for columnName. .
columnName Name of the column identifying the relation. Objects from the childObjects collection will be added as related for the column in the table containing parentObjectId. 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 "columnName" 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
childrenObjectIds a collection of child objectId values to set into the relation. Table of the child objects is identified by columnName.

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 columnName argument - notice the relation column name is "address". If it were specified as "address:Address", then the column would be created automatically.
relation-column

DataStoreFactory *dataStore = [Backendless.shared.data of:[Person class]];
[dataStore addRelationWithColumnName:@"address:Address:1" parentObjectId:@"XXXXX" childrenObjectIds:@[@"YYYYY"] responseHandler:^(NSNumber *relations) {
    NSLog(@"Relation has been added");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let dataStore = Backendless.shared.data.of(Person.self)
dataStore.addRelation(columnName: "address:Address:1", parentObjectId: "XXXXX", childrenObjectIds: ["YYYYY"], responseHandler: { relations in
    print("Relation has been added")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Person"];
[dataStore addRelationWithColumnName:@"address:Address:1" parentObjectId:@"XXXXX" childrenObjectIds:@[@"YYYYY"] responseHandler:^(NSNumber *relations) {
    NSLog(@"Relation has been added");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let dataStore = Backendless.shared.data.ofTable("Person")
dataStore.addRelation(columnName: "address:Address:1", parentObjectId: "XXXXX", childrenObjectIds: ["YYYYY"], responseHandler: { relations in
    print("Relation has been added")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})