Can't relate user to Children

Hello

I am using the Android SDK. I have the user added to the user table and I have the children added to the Booking table. Now when I try to set the relation, I keep getting
BackendlessException{ code: ‘1303’, message: ‘Unable to create relation. Child objects with ids ‘null’,‘null’ are not found in the related table.’ }

HashMap<String, Object> parentObject = new HashMap<String, Object>();
parentObject.put("objectId", GlobalVar.myUser.getUserObjectId());
ArrayList<Map> children = new ArrayList<Map>();
for (Booking booking : bookings_arr) {
    HashMap<String, Object> childObject = new HashMap<String, Object>();
    childObject.put("objectId", booking.getObjectId());
    children.add(childObject);
}


ResultReturn results = new ResultReturn();
BackendlessUser user = Backendless.UserService.CurrentUser();


try {
    Integer val = Backendless.Data.of(BackendlessUser.class)
.setRelation(user, "Booking", children);

Hello,

Are you using Backendless 4?

Mark

Yes i am using backendless 4. And I debugged the code the verified that the children actually has object ID so it is not null

Establishing a relation in Backendless 4 is done differently than in 3.x. You can use either the addRelation or the setRelation API. Please see the documentation: https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android

I never used backendless 3. I am using the Set relation (as shown above) as per the documentation ( as far as I know). Infact the set relationship between data tables works fine. It is just for some reason from User table to data table returns the above exception

Does the '“Booking” column already exist in the Users table?

Does the method return an integer?

Yes it does and it has 1:N relationship

The method throws an exception so no integer thrown as it jumps to the exception block when it executes

The problem is in the code that creates the “children” collection. If you change it as shown below, it should work:

children.add(booking);

Please see the doc for details: https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android

Thank you. I will try in a bit and report back.

But the docs say
Integer result = Backendless.Persistence.of( “TABLE-NAME” ).setRelation( Map parentObject, String relationColumnName, Collection<Map> children );

So shouldn’t it take Collection of Maps (like any other relations adding between data tables)?

Hello Mark,

Oh wow!!! That worked!! I think I understand what’s going on here.
In order to add a relation to User Table, you can’t use Backendless.Data.of(“Users”) as it complains you have pass BackendlessUser.class.

And the fact that we are forced to that, it makes the whole thing follow the “custom class” way meaning I can no longer set relation using Map and I have to set it by passing the object.

Does this understanding make sense? Maybe it would be beneficial to add section about that in docs for the “relation with Users tables” OR enable us to use Maps with user tables to satisfy all use cases (such the case when you don’t have an object such as Booking where it maps perfectly to the table)

Thanks again Mark

Thank you for confirming that it works. I made a note to revisit the behavior so we can update the docs with more precise details for the Users table.

Cheers,
Mark