On duplicate entry - update?

Hi!

I just have a quick question. Do you have any save/update method that can on duplicate entry update the fields for the already existing entry or do I need to first check if it’s available in the database already, fetch that object and then update it?

Thanks
Tobias

You need to know the objectId, then you can save the object which results in an update. If you do not know the objectId you would create a New object when saving, no matter if data is the same.

Thanks for the response!

Since one field in my table is “Unique Value” and “Indexed” as constraint it won’t create a new object for me. It throws an exception instead.

So the reason why I asked for on duplicate entry - update was to figure out if there was another way around the exception besides knowing the objectId.

But I guess then I’ll have to stick with doing first one access to check if available, if not create a new else update the old one. To bad, this requires 2 requests to the database and not only one, well well!

Thanks again for your response :slight_smile:

You can work around this by just creating a custom service.

Send your data to the service, check if exists, create if not, respond to client. = 1 client call.

It’s few lines of code, done in 2 minutes.

Cheers,
jens

Jens, could you please explain more how to update object when you know objectId?

Create an object with the same object id and just save it.

I got this exception BackendlessFault{ code: ‘IllegalArgumentException’, message: ‘null’ }

Post your related code please.

 List<Person> listOfFriends = relationship.getFriends();
    listOfFreiends.add(newPerson);
    
    Relationship newrelationship = new Relationship();
    newrelationship.setObjectId(relationship.getObjectId());
    newrelationship.setFriends(relationship.getFriends());
    newrelationship.setPendings(relationship.getPendings());
    newrelationship.setRequests(relationship.getRequests());
   newrelationship.saveAsync(new AsyncCallback<Relationship>() {
        public void handleResponse(Relationship savedContact) {
            Log.v("USPJELI SMO", "USPJELI SMO");
        }


        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            Log.v("No, it doesn't work", backendlessFault.toString());
        }
    });
}

Try

Backendless.Data.of(Relationship .class).save(newRelationShip,Asynccallback()....)

Same exception :confused:

Now I got ,BackendlessFault{ code: ‘36’, message: ‘Duplicate entry for column ‘user’’ }"

Have you tried this?

List<Person> listOfFriends = relationship.getFriends();
listOfFreiends.add(newPerson);
relationship.saveAsync(new AsyncCallback<Relationship>() {
public void handleResponse(Relationship savedContact) {
Log.v("USPJELI SMO", "USPJELI SMO");
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Log.v("No, it doesn't work", backendlessFault.toString());
}
});
}

Same problem :frowning:

Could you attach the source code for the Person and Relationship classes to this topic please?

Hey, there is a code. Thank you all for help!

Relationship.txt (5.77kB)

Person.txt (4.77kB)

Mladen,

I just tried exactly the same code you posted and it worked for me. Here’s what I have (this is using your Person and Relationship classes without any changes):

        final AsyncCallback<Relationship> saveRelationshipCallback = new AsyncCallback<Relationship>() {
            public void handleResponse(Relationship savedContact) {
                Log.v("USPJELI SMO", "USPJELI SMO");
            }
            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                Log.v("No, it doesn't work", backendlessFault.toString());
            }
        };


        AsyncCallback<Relationship> getRelationshipCallback = new AsyncCallback<Relationship>() {
            @Override
            public void handleResponse(Relationship relationship) {
                Relationship newrelationship = new Relationship();
                newrelationship.setObjectId(relationship.getObjectId());
                newrelationship.setFriends(relationship.getFriends());
                newrelationship.setPendings(relationship.getPendings());
                newrelationship.setRequests(relationship.getRequests());
                newrelationship.saveAsync( saveRelationshipCallback );
            }


            @Override
            public void handleFault(BackendlessFault fault) {
                Log.e( "APP ERROR", fault.getMessage() );
            }
        };


        Backendless.Data.of( Relationship.class ).findFirst( getRelationshipCallback );

On the server-side I have the following table definitions:

Person:
http://support.backendless.com/public/attachments/ffe12596eae6829c04b8b1176d093751.png</img>

Relationship:
http://support.backendless.com/public/attachments/a4610eed270fa6f36a11c9d5d7e9f3cb.png</img>

I pre-created data in the tables using console so there is something I could work with:
Person data:
http://support.backendless.com/public/attachments/9e5b655480785631629aafe668ec703f.png</img>

Relationship data:
http://support.backendless.com/public/attachments/4fad3d1c32f128804fbefd243dd56222.png</img>

Would be glad to test it against your app - I would need your app id and Android secret key.

Regards,
Mark

Hey, I have tried your code. I got relationship class but when I do

newrelationship.saveAsync( saveRelationshipCallback );

i still get this error: { code: ‘36’, message: ‘Duplicate entry for column ‘user’’ }

I can retrieve (load) Relationship instance of class but when I one to change it and save (update) I god this error.

Mark, how I can send you those information for test? Can you please post your email?