Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UpdatePolicy.ALL for RealmList.add #849

Closed
t7costa opened this issue May 26, 2022 · 1 comment · Fixed by #864
Closed

Support UpdatePolicy.ALL for RealmList.add #849

t7costa opened this issue May 26, 2022 · 1 comment · Fixed by #864
Assignees

Comments

@t7costa
Copy link

t7costa commented May 26, 2022

It is currently not possible with RealmList to add multiple items with the same primary key (or for different lists to add an item with the same primary key)
RealmListInternal uses UpdatePolicy.ERROR for its insert operators (https://github.com/realm/realm-kotlin/blob/master/packages/library-base/src/commonMain/kotlin/io/realm/internal/RealmListInternal.kt#L197) which differs from the behaviour in realm-java (which uses copyToRealmOrUpdate when adding unmanaged objects to a managed RealmList https://github.com/realm/realm-java/blob/master/realm/realm-library/src/main/java/io/realm/RealmList.java#L214)

class TestList: RealmObject {
    var items: RealmList<TestItem> = realmListOf()
}

class TestItem : RealmObject {
    @PrimaryKey var id: String = "1"
}
realm.write {
    val myList = copyToRealm(TestList())
    myList.items.add(TestItem())
    myList.items.add(TestItem())
}
realm.write {
    val testList1 = copyToRealm(TestList())
    val testList2 = copyToRealm(TestList())

    testList1.items.add(TestItem())
    testList2.items.add(TestItem())
}

java.lang.IllegalArgumentException: Failed to create object of type 'TestItem': RealmCoreException([33]: Object with this primary key already exists)

@cmelchior
Copy link
Contributor

Hi @t7costa
Thank you for this. You are correct, we should have the same semantics as Realm Java. We will fix this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants