How Get Child Column Of A One To Many Relation

I have a Users class with a collection column titled ‘following’. Many Users can follow one artist of the Artist Class. Now, in my Artist class I have a column written Users.following that was generated from the relation I made from the User Class from that following column.

How then do i retrive the users following an Artist in my artist class i.e Users.following? I have tried the following and i get the error (Server reported an error: Optional(FAULT = ‘1023’ [Unable to retrieve data. Query contains invalid object related properties.] <Unable to retrieve data. Query contains invalid object related properties.> )):

let dataQuery = BackendlessDataQuery()

    dataQuery.whereClause = "artist = \'\("Ngoni")\'"

    let queryOptions = QueryOptions()

    queryOptions.related = ["Users.following" ,"Users"]; //I even tried vice-versa

    dataQuery.queryOptions = queryOptions

    

    var error: Fault?

    let bc = backendless.data.of(Artist.ofClass()).find(dataQuery, fault: &error)

    if error == nil {

        print("Orders have been retrieved: \(bc.data)")

    }

    else {

        print("Server reported an error: \(error)")

    }

I have also tried making a variable in my Swift definition class called Users.followers BUT because of the ‘dot’ in Users.followers it suggests i place a comma which becomes totally wrong. Please help sob sob

Users.following is not a relation, it only shows the objects which have this object as relation. So, you cannot load “child of” objects.

Oh alright, so how do I retrieve these User.following objects sir?

…can i get samplecode of how i am supposed to retrieve it?

Please provide your Artist class here.

//I have also attached what it looks like on Backendless. Below is how i defined it in Swift. The last line of code there that is commented out like i side gives me issues because of that ‘dot’. Thank You So Much Vyacheslav

import Foundation

class Artist : NSObject

{

var artist : String?

var genre : String?

var followers: Int?

var plays: Int?

var image : String?

var objectId : String?

var created : NSDate?

//var Users.following : User?

}

As the documentation states, fields of type Int cannot be optional:

https://backendless.com/documentation/data/ios/data_data_object.htm

First, I’ve fixed your class - you should use initialized numeric & boolean property:

class Artist : NSObject
{
var artist : String?
var genre : String?
var followers: Int = 0
var plays: Int = 0
var image : String?
var objectId : String?
var created : NSDate?
}

And here is a code to retrieve the artist followers:

let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = "following.artist = \'\("Ngoni")\'"
var error: Fault?
let bc = backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
if error == nil {
print("Followers have been retrieved: \(bc.data)")
}
else {
print("Server reported an error: \(error)")
}

Vyacheslav, I’m sorry to say but the query you made for me there is querying the other way round for me. It gives me the artists I follow as a user. But instead, I want the users that follow a certain artist. Please find attached my Users Class. I created a collection of artists I follow from the Artist class. And by doing so, Backendless automatically created Users.following in my Artist class, which for an artist is the Users who are following that artist. So I want to get these followers who follow this certain artist, Ngoni in our previous example. When I ran your code it gave me an object of the people i follow and my phone number etc as below:

Followers have been retrieved: [<BackendlessUser> {

Phone = 0769968319;

"___class" = Users;

"__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"country\",\"eventsGoing\",\"created\",\"ownerId\",\"password\",\"Phone\",\"following\",\"name\",\"___class\",\"eventNotGoing\",\"updated\",\"objectId\",\"eventsMaybe\",\"likes\"],\"relatedObjects\":{\"eventsGoing\":[\"ydnalPGSb7\"],\"following\":[\"a6St8vjaDD\",\"jSBAVivc4a\",\"lbt6gqg56Q\",\"pZ1UP4LZXQ\",\"uMrRvyUZ0o\",\"WnqnUKRHf4\"],\"eventsMaybe\":[\"r8AF46ekNW\"],\"likes\":[\"C892A607-AFAE-AEE5-FF59-ECF76DC41E00\"]}}";

country = "&lt;null&gt;";

created = "2016-02-27 21:29:10 +0000";

email = "&lt;null&gt;";

eventNotGoing =     (

);

eventsGoing =     (

    "&lt;Yzia.Shows: 0x7f87d26b4bd0&gt;"

);

eventsMaybe =     (

    "&lt;Yzia.Shows: 0x7f87d26c18e0&gt;"

);

following =     (

    "&lt;Yzia.Artist: 0x7f87d26b6b50&gt;",

    "&lt;Yzia.Artist: 0x7f87d26b7c30&gt;",

    "&lt;Yzia.Artist: 0x7f87d26b8e30&gt;",

    "&lt;Yzia.Artist: 0x7f87d26b9f50&gt;",

    "&lt;Yzia.Artist: 0x7f87d26bb1a0&gt;",

    "&lt;Yzia.Artist: 0x7f87d26bc370&gt;"

);

lastLogin = "2016-04-27 21:02:51 +0000";

likes =     (

    "&lt;Yzia.Songs: 0x7f87d26c32b0&gt;"

);

name = mungandi;

objectId = "F00F902C-1351-BD0A-FFDD-4A291F323900";

ownerId = "&lt;null&gt;";

password = "&lt;null&gt;";

updated = "2016-04-27 18:05:10 +0000";

userStatus = ENABLED;

}]

I hope you understand what I need and how my classes are structured now.

Here is what my User class looks like, find attached

So I was hoping to get a list of the Users that follow an Artist just like the way is is on Instagram where it shows you who is following that certain person, in our case the Artist and that we can retrieve from Users.following in the artist class. I hope I am making sense (/_)

"....And by doing so, Backendless automatically created Users.following in my Artist class....

No, no, no!! That is NOT a relation, it is not a collection. It is nothing but a CLUE for you as a developer that the given entity is related to another one. It says right there “CHILD OF”. Look closer at the console. Relations are marked as such - it says in the console “RELATION”. When you see “child of”, it is not a relation.

Now, to answer your question: how to get users who have a specific artist in the relation called “following”. Here’s a query you should use:

following.objectId = 'your-artist-objectId'

That query should be sent to the Users table.

I’m really sorry Mark I had that blunt misconception. I’m fairly new to the platform and I am keen to learn :slight_smile: Anywho, so I ran this code and I still get an object with my user details. I changed just that one line from

dataQuery.whereClause = "following.artist = \'\("artistname")\'"

to:

dataQuery.whereClause = "following.objectId = \'\("








uMrRvyUZ0o")\'"

What am I missing?

    let dataQuery = BackendlessDataQuery()

    dataQuery.whereClause = "following.objectId = \'\("uMrRvyUZ0o")\'"  

    var error: Fault?

    let bc = backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)

    if error == nil {

        print("Followers have been retrieved: \(bc.data)")

    }

    else {

        print("Server reported an error: \(error)")

    }

It gives me:

Followers have been retrieved: [<BackendlessUser> {
Phone = 0769968319;
“___class” = Users;
“__meta” = “{“relationRemovalIds”:{},“selectedProperties”:[“country”,“eventsGoing”,“created”,“ownerId”,“password”,“Phone”,“following”,“name”,”___class",“eventNotGoing”,“updated”,“objectId”,“eventsMaybe”,“likes”],“relatedObjects”:{“eventsGoing”:[“ydnalPGSb7”],“following”:[“a6St8vjaDD”,“jSBAVivc4a”,“lbt6gqg56Q”,“pZ1UP4LZXQ”,“uMrRvyUZ0o”,“WnqnUKRHf4”],“eventsMaybe”:[“r8AF46ekNW”],“likes”:[“C892A607-AFAE-AEE5-FF59-ECF76DC41E00”]}}";
country = “<null>”;
created = “2016-02-27 21:29:10 +0000”;
email = “<null>”;
eventNotGoing = (
);
eventsGoing = (
“<Yzia.Shows: 0x7fa9923baa00>”
);
eventsMaybe = (
“<Yzia.Shows: 0x7fa9923c74b0>”
);
following = (
“<Yzia.Artist: 0x7fa9923bc220>”,
“<Yzia.Artist: 0x7fa9923bd3c0>”,
“<Yzia.Artist: 0x7fa9923be680>”,
“<Yzia.Artist: 0x7fa9923bf800>”,
“<Yzia.Artist: 0x7fa9923c0a50>”,
“<Yzia.Artist: 0x7fa9923c1c20>”
);
lastLogin = “2016-04-27 21:02:51 +0000”;
likes = (
“<Yzia.Songs: 0x7fa9923c8e80>”
);
name = mungandi;
objectId = “F00F902C-1351-BD0A-FFDD-4A291F323900”;
ownerId = “<null>”;
password = “<null>”;
updated = “2016-04-27 18:05:10 +0000”;
userStatus = ENABLED;

}]

You retrieved a collection of users which contains only one BackendlessUser object.

Thank You Mark! I see it Now :slight_smile: