Adding a new Relation - Confusion

So I have created a new database table under the name of ‘ColorPalette’. That table contains a few columns, from which the one is called ‘likes’. That column is a relational column. And it’s referencing the actual Users table.

Now from my Android app, I’m trying to update that relational column (likes), whenever a user clicks a button. And when that happens a new value (User) inside that relational column should appear.

Now I’m confused how to achieve that exactly, because whenever I execute that, I receive all my users inside that relational column (likes) and not just the single currently logged in user like I want.

Backendless.Data.of(ColorPalette::class.java)
                .addRelation(
                    ColorPalette(
                        objectId = "037EF750-1AF7-4AC6-8CA1-47315B38D2D7",
                        likes = listOf(
                            Users(
                                id = "106446187796280519744"
                            )
                        )
                    ),
                    "likes",
                   "",
                    object : AsyncCallback<Int> {
                        override fun handleResponse(response: Int?) {
                            Log.d("BackendlessDataSource", "$response")
                        }

                        override fun handleFault(fault: BackendlessFault?) {
                            Log.d("BackendlessDataSource", fault?.message.toString())
                        }
                    })

Here I’m passing the actual User Id of the user I want to add to that relational column (likes). I’m getting that User Id from the currently logged in user bw: Backendless.UserService.CurrentUser().userId
And also for the testing purpose I’ve hard-coded the actual objectId of the row/entry which I want to update.

Hi. Please describe more precisely what the type of relation you created. (1:1, 1:n)

whenever I execute that, I receive all my users inside that relational column

The code you posted shouldn’t return any objects. Do you perform some additional request?

Please, note the difference between the setRelation and addRelations methods.

I’m having 1:N relation. Many users will be able to like. What’s the main difference between addRelation and setRelation, because I don’t see any documentation with the SDK directly. It would be great if we would see the docs right into the code. :slight_smile:

I’m not sure how to add a new relational data inside a Database (from Android SDK), where that data is a relational data of a User. Is there any example?

Hello @Stefan_Jovanovic

Didn’t doc help you?

Regards,
Viktor

1 Like