Related Object Properties from one-to-many relation

Hi,

I am following the Social App blog post (https://backendless.com/how-to-build-social-app-backend-backendless-1/)

From the post.likes relation column (which is a one-to-many to users table) we want to load only the users.objectId property

This is my query on the Posts table (notice the “likes.objectId as likedBy” property)

val queryBuilder = DataQueryBuilder.create()
        queryBuilder.whereClause =  "ownerId= '$userId' OR ownerId in (Users[objectId='$userId'].following.objectId)"
        queryBuilder.addProperties("title", "content", "image", "location",
            "locationName", "created", "updated", "ownerId", "objectId",
            "postOwner.name as postUserName",
            "postOwner.imageUrl as postUserImage",
            "Count(likes) as totalLikesCount",
            "likes.objectId as likedBy"
            ).setGroupBy("objectId")
            .setSortBy("created desc")
            .setRelated("postOwner")
            .setRelated("likes")

but all likes/user properties are loaded

{
  "content": "My First Post",
  "created": "Oct 15, 2020 09:48:03",
  "image": "https://backendlessappcontent.com/...",
  "likes": [
    {
      "properties": {
        "lastName": "-",
        "lastLogin": "Oct 15, 2020 13:26:28",
        "userStatus": "ENABLED",
        "created": "Oct 11, 2020 08:01:50",
        "county": "HUNEDOARA",
        "ownerId": "88F9E311-9B24-4B47-9EDC-43AD6B1A7BB7",
        "socialAccount": "BACKENDLESS",
        "firstName": "-",
        "___class": "Users",
        "blUserLocale": "en",
       "updated": "Oct 11, 2020 08:07:17",
        "email": "costi@leadingedge.ro",
        "objectId": "88F9E311-9B24-4B47-9EDC-43AD6B1A7BB7"
      }
    }
  ],
  "location": {
    "x": 22.9072331,
    "y": 45.7678128,
    "srs": "WGS84"
  },
  "objectId": "A8A286BB-F389-4653-A7A8-EE2EF2FCE0FC",
  "ownerId": "917361FD-D6F7-4881-8250-1F59370BBEF1",
  "title": "First Post",
  "totalLikesCount": 3
}

We would however expect to see only the object id property, like this

{
  "content": "My First Post",
  "created": "Oct 15, 2020 09:48:03",
  "image": "https://backendlessappcontent.com/...",
  "likes": [
    {
      "properties": {
        "objectId": "88F9E311-9B24-4B47-9EDC-43AD6B1A7BB7",
      }
    }
  ],
  "location": {
    "x": 22.9072331,
    "y": 45.7678128,
    "srs": "WGS84"
  },
  "objectId": "A8A286BB-F389-4653-A7A8-EE2EF2FCE0FC",
  "ownerId": "917361FD-D6F7-4881-8250-1F59370BBEF1",
  "title": "First Post",
  "totalLikesCount": 1
}

Hope this makes sense and any help is much appreciated

Thanks
Constantin

Hi Constantin,

What happens to the result if you remove the following line from the queryBuilder composition:

.setRelated("likes")

?

Regards,
Mark

Hi Mark,
I removed it and no likes properties are returned in the result now

{
  "content": "Test Post",
  "created": "Oct 15, 2020 09:48:03",
  "image": "https://backendlessappcontent.com/",
  "liked": false,
  "location": {
    "x": 22.9072331,
    "y": 45.7678128,
    "srs": "WGS84"
  },
  "locationName": "",
  "loggedUserIsOwner": false,
  "objectId": "A8A286BB-F389-4653-A7A8-EE2EF2FCE0FC",
  "ownerId": "917361FD-D6F7-4881-8250-1F59370BBEF1",
  "postOwner": {
    "properties": {
      "lastName": "User",
      "lastLogin": "Oct 15, 2020 14:57:24",
      "gdprConsent": false,
      "userStatus": "ENABLED",
      "bikerSince": 2016,
      "created": "Sep 22, 2020 20:41:27",
      "county": "",
      "ownerId": "917361FD-D6F7-4881-8250-1F59370BBEF1",
      "socialAccount": "GOOGLE_PLUS",
      "firstName": "David",
      "public": true,
      "imageUrl": "https://backendlessappcontent.com/",
      "name": "David",
      "___class": "Users",
      "blUserLocale": "en",
      "id": "116714605064186866884",
      "updated": "Oct 15, 2020 07:24:58",
      "email": "",
      "objectId": "917361FD-D6F7-4881-8250-1F59370BBEF1"
    }
  },
  "postUserImage": "https://backendlessappcontent.com/...",
  "postUserName": "David",
  "title": "Test Post",
  "totalLikesCount": 5
}

Mark, the reason I need all the post likes.user ids is so I can loop through them to see if current logged user has liked the post

Thanks
C.

Hello @Constantin_Craciunescu

Have you seen the next part of this article https://backendless.com/how-to-build-a-social-app-backend-backendless-2/ ?

There is a part of how to implement this.

Regards, Vlad

to load all the users ids it’s not good idea because there might be a ton of users and it will be a slow request, much better just to check if the current user is exited in the relations

Hi Valdimir
Totally agree with being too slow to load all the user id’s

And yes, I did looked at the at the code in the second part but we use the Android SDK and we plan to give it a go with that a approach.

much better just to check if the current user is exited in the relations

I think the code in the blog article checks if the post objectId exists in curent user liked posts

Thanks,
C.

yes, you can find it in the async function loadPosts(currentUserId, whereClause) API method

1 Like