Backendless Support
 
Answered

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.' }

  1. HashMap<String, Object> parentObject = new HashMap<String, Object>();
  2. parentObject.put("objectId", GlobalVar.myUser.getUserObjectId());
  3. ArrayList<Map> children = new ArrayList<Map>();
  4. for (Booking booking : bookings_arr) {
  5. HashMap<String, Object> childObject = new HashMap<String, Object>();
  6. childObject.put("objectId", booking.getObjectId());
  7. children.add(childObject);
  8. }
  9. ResultReturn results = new ResultReturn();
  10. BackendlessUser user = Backendless.UserService.CurrentUser();
  11. try {
  12. Integer val = Backendless.Data.of(BackendlessUser.class)
  13. .setRelation(user, "Booking", children);

Best Answer
photo

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

  1. children.add(booking);

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

Leave a Comment

Comments (10)

photo
1

Hello,

Are you using Backendless 4?

Mark

photo
1

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

photo
1

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

photo
1

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

photo
1

Does the '"Booking" column already exist in the Users table?

Does the method return an integer?

photo
1

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

photo
1

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

  1. children.add(booking);

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

photo
1

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)?

photo
1

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

photo
1

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