Is good practice update user relationship with object if object have the ownerId?

Hi,

I go to try to explain my question…

I have an app where I want to do a relationship between users and comments and another table. I can update comments with my relation table, but I want to update the comment with the user, too. I was trying but I don’t be able to do it. Then, I see that in comment table I have a column ownerId that is the id of the user that is updating the comment. My question at this point is… if in every comment that I’m updating, in the comment table is set the ownerId that is the Id of the current user, have sense to do a relationhip between comments and users??

If the anwer is yes, how can I do it?

I’ve attached a picture of my comment table that maybe help to see my question.

This is the code that I was trying and that don’t work:

currentUserId = Backendless.UserService.loggedInUser();

    final BackendlessDataQuery query = new BackendlessDataQuery();
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setRelationsDepth(1);
    query.setQueryOptions( queryOptions );
    query.setWhereClause( "objectId = " + "'" + beachId + "'" );


    final BackendlessDataQuery queryUser = new BackendlessDataQuery();
    QueryOptions queryOptionsUser = new QueryOptions();
    queryOptionsUser.setRelationsDepth(1);
    queryUser.setQueryOptions( queryOptionsUser );
    queryUser.setWhereClause( "objectId = " + "'" + currentUserId + "'" );




    new Thread(new Runnable() {
        public void run() {
            beachToUpdate = Backendless.Persistence.of( Beach.class ).find(query).getData().get(0);
            userToUpdate = Backendless.Persistence.of( Users.class ).find(queryUser).getData().get(0);
        }
    }).start();




    updateComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setComment();
        }
    });
}


public void setComment(){
    comment = new Comment();
    comment.setMessage("trying to update a comment with user");
    comment.setAuthorEmail(preferences.getString(MainActivity.USER_NAME, ""));
    userToUpdate.getComment().add(comment);
    beachToUpdate.getComment().add(comment);


    new Thread(new Runnable() {
        public void run() {
            Backendless.Persistence.save(beachToUpdate);
            Backendless.Persistence.save(userToUpdate);
        }
    }).start();
}

Hi Salva,

The ownerId field is not a relation, but just a string column , which contains the objectId value of the user who created the record. Note that this value is not updated if another user updated the same record.

I would suggest you to store also a relation to the Users table, as it gives you much more flexibility.

Hi Sergey,

Thanks for your reply. Yes, ok.

According with the code that I type above, can you provide me some small idea of how to do it?? Because I was trying to update with the user relation (in the way that you can see in my code) and I don’t be able to do it. And in the documentation don’t find any clear example that I can understand about this.

Thanks!

You should make sure that the userToUpdate is first loaded, then you add a comment to it, and then you save it. As long as you do parts of this in separate threads, this sequence may be disrupted, so you better add thread synchronization or use async methods from SDK.

Here you may find related sample code: https://backendless.com/documentation/data/android/data_relations_save_update.htm

Hi Sergey,

Ok, I understand your indications, but I think that I need more some help. I’m getting this error when I’m trying to update the user in the relationship table comment:

BackendlessException{ code: ‘3045’, message: ‘Unable to update user. Required fields are empty.’ }
at com.backendless.Invoker$SyncResponder.errorHandler(Invoker.java:127)
at com.backendless.core.responder.AdaptingResponder.handledAsFault(AdaptingResponder.java:120)
at com.backendless.core.responder.AdaptingResponder.responseHandler(AdaptingResponder.java:75)
at weborb.client.ioEngine.HttpIOEngine.processAMFResponse(HttpIOEngine.java:262)
at weborb.client.ioEngine.HttpIOEngine.send(HttpIOEngine.java:207)
at weborb.client.ioEngine.HttpIOEngine.invoke(HttpIOEngine.java:145)
at weborb.client.WeborbClient.invoke(WeborbClient.java:138)
at com.backendless.Invoker.invokeSync(Invoker.java:100)
at com.backendless.Persistence.save(Persistence.java:111)

This is the code the I’m using (Yes, I have to change to asyncronous call but I want to do that work first)

currentUserId = Backendless.UserService.loggedInUser();

final BackendlessDataQuery query = new BackendlessDataQuery();
QueryOptions queryOptions = new QueryOptions();
queryOptions.setRelationsDepth(1);
query.setQueryOptions( queryOptions );
query.setWhereClause( "objectId = " + “’” + beachId + “’” );

final BackendlessDataQuery queryUser = new BackendlessDataQuery();
QueryOptions queryOptionsUser = new QueryOptions();
queryOptionsUser.setRelationsDepth(1);
queryUser.setQueryOptions( queryOptionsUser );
queryUser.setWhereClause( "objectId = " + “’” + currentUserId + “’” );

new Thread(new Runnable() {
public void run() {
beachToUpdate = Backendless.Persistence.of( Beach.class ).find(query).getData().get(0);
userToUpdate = Backendless.Persistence.of( Users.class ).find(queryUser).getData().get(0);
}
}).start();

comment = new Comment();
comment.setMessage(getCommentFromUser);
comment.setScore(setFinalRatingNumber);
comment.setAuthorEmail(preferences.getString(MainActivity.USER_NAME, “”));
userToUpdate.getComment().add(comment);
beachToUpdate.getComment().add(comment);

new Thread(new Runnable() {
public void run() {
Backendless.Persistence.save(beachToUpdate);
Backendless.Persistence.save(userToUpdate);
}
}).start();

The error message talks for itself: seems you failed to set some required user’s fields (email by default, and maybe you’ve set up some additional ones).

Looking for an error in your application is not a free support option due to the Support Policy, but if you prepare a sample application which would show that there’s a bug in Backendless - we’ll be happy to help you.

Hi Sergey.

Only one thing if you can answer. I’ve set the required field, yes, was email. Now I’m getting

                 BackendlessException{ code: '1020', message: 'Missing ___class property for entity: properties' }

I’m looking same error in other support tickets and that is rare for me is that in other errors the ‘properties’ usually have the name the propertie where is the issue.

I don’t know if you can help me with this. Just in case, I’ve attached you a screenshot of my properties.

This is that I’m doing:

userToUpdate.setEmail(“myemail@gmail.com”);
userToUpdate.setProperty(“comment”, comment);
userToUpdate.getComment().add(comment);

Try adding the following line right after you do initApp():

Backendless.Data.mapTableToClass( "Comment", Comment.class );

(in case your class name is Comment)

If it doesn’t help, please create a new topic so that we can investigate this issue separately.

Hi sergey,

Don’t work adding this line. I get another different error:

BackendlessException{ code: ‘Server.Processing’, message: ‘java.lang.RuntimeException: java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘B95DF41D-ED7C-B947-FF03-3740ECB12C00.EFFACCCA-8641-CD50-F.25810F45-C117-9465-F.EA7E1’ doesn’t exist’ }

And yes, the name of my class is Comment.class. I go to open a new topic