Support Topics Documentation Slack YouTube Blog

Relations Android

I’m having trouble understanding how to work with relations.

I have two tables, a Users table and a Groups table, from the App I can create users and groups, but I’m having trouble at adding users to a group.

This is my users table example:

And this the groups table:

I want to be able to add users to a group, for example the user “test” to the “test” group, this of course on Android Studio.

I’ve tried this code but the relations doesn’t change. (I’ve edited Users to users on the groups)

Hello,

I noticed you declared the column called “Users”, but in the code you use it as “users”:

Please make sure it is consistent.

Regards,
Mark

Hi Mark thanks for answering, after that print I’d already changed the table to a lowercase, so that line of code is actually right.
Just to be sure…

You may want to change “setRelation” to “addRelation” since your “users” column is a to-many relation. and I don’t know about how Android works but on iOS, it’s ok to set the column name as just “users” and instead of passing the children objects, you can try just passing an array of objectIds. Try these options and see if any of those changes the behavior.

Hi Jonathan, I’ve tried changing it to “addRelation” and nothing seems to change, and about the array thing, isn’t that what I’m doing with “ArrayList children = new ArrayList<>();” ??

Thank you a lot,

Alexandre

instead of “children.add( childObject );” try doing “children.add( userObjectId )”

I’m not sure if that will help, but that’s what I do on iOS. I pass just the objectId of the child and not the entire child object.

No change either…
Those this “Object userObjectId = user.getObjectId();” get me the objectId correctly??

you will have to debug to see if that is returning a UID string.

I was just browsing the Android docs and it is saying to use: “Backendless.Persistence.of” instead of “Backendless.Data.of”

https://backendless.com/docs/android/data_set_relation_with_objects_android.html

Still not working :frowning: thanks so much for the help tho… I really thought that was the problem now, because I’d actually seen that documentation and just forgot to change my code.

Hey, @Alexandre_Coutinho

I’ve just tried running your code in my app and everything worked just fine.
Make sure you are using 5.2.4 version of Android SDK. For a clean test try replacing values in HashMap keys with actual values like followed:

        HashMap<String, Object> parentObject = new HashMap<String, Object>();
        parentObject.put( "objectId", "15C071C8-7B75-56AE-FF08-1145C67A2200" );

        HashMap<String, Object> childObject = new HashMap<String, Object>();
        childObject.put( "objectId", "99C5E357-9F7B-22F7-FF0C-6E7E93E00500" );

        ArrayList<Map> children = new ArrayList<Map>();
        children.add( childObject );

        Backendless.Data.of( "Groups" ).setRelation( parentObject, "users:Users:n", children,
                new AsyncCallback<Integer>()
                {
                    @Override
                    public void handleResponse( Integer response )
                    {
                        Log.i("MYAPP", "Related objects added: " + response);
                    }

                    @Override
                    public void handleFault( BackendlessFault fault )
                    {
                        Log.i("MYAPP", "Error: " + fault);
                    }
                } );

Parent objectId - id of the “Group” table object
Child objectId - id of a user in Users table

Relation column should be set in “Groups” to Users. If that will work - debug your project and find out where things are going wrong.
Anyways please come back with results :slight_smile:

Anton

Hey, @Alexandre_Coutinho

Did you try the approach I proposed? Got any results? Can this topic be closed?

Anton

The code used has you presented did work, but my code still doesn’t, I need it to do all the saving and finding in one go.

Saving and finding are two separate API operations. You cannot execute them “in one go”