question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

save() overrides primary instead of updating object

See original GitHub issue

Hi, so this is my class:

@AutoIncrementPK
open class Contact() : RealmObject() {
    @PrimaryKey
    var id: Long? = null
    var name: String = ""
    var publicKey: String = ""

    constructor(name: String, publicKey: String) : this() {
        this.name = name
        this.publicKey = publicKey
    }
}

I tried to do an update on an object like this:

contact.name = name
contact.save()

And instead of updating the object, a new object with these properties and new id is created. I checked the code and obviously the line initPk in save overrides the primary. I don’t think that this is intended to happen, is it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vicpinmcommented, Nov 2, 2017

@osterbaer I can check the primary key value, and only if is null, I can generate a pk for that entity. If not, I would keep the pk. Is that ok for your use case?

0reactions
vicpinmcommented, Dec 4, 2017

@benjaminledet @kvacquier @osterbaer 2.0.0 final version is published now, with the change in primary key generation. Now, only pk is auto generated if has null value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django tries to INSERT instead of UPDATE on save()
UUIDField(primary_key=True, default=uuid.uuid4) , calling save will always result in an INSERT rather than an UPDATE.
Read more >
Learn the subtle differences between Save and Update in ...
The .save() method is used to write a model instance to the database. It can be an existing record or even a new...
Read more >
Spring Jpa- Instead of updating a row, I'm accidentally adding ...
I'm using .save() to update a row, but instead of updating its adding new row in ... u/Override public void updateTasks(Task task) {...
Read more >
Updating an object without overwriting existing attributes #4882
I'm having trouble where by if I insert an unmanaged instance of an object which uses the same primary key of an object...
Read more >
Hibernate's persist(), save(), merge() and update()
The Javadoc of Hibernate's save method states that it generates the primary key value first: Persist the given transient instance, first assigning a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found