You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When trying to remove entities from a One-To-Many relationship, they keep reappearing in the database after put() when first removing from the relationship and afterwards from box
Basic info (please complete the following information):
Create 2 entities with a 1:N relationship (one and many)
Add some entities to the ToMany (many) of the other (one).
Remove 1 entity from the relationship with one.many.removeById(id)
Remove the entity from the box box.remove(id)
Put the parent (one) in the box again (overriding it)
Expected behavior
No matter the order, if an entity gets deleted from the box and the relation and is nowhere to be found, it should not reappear in the box.
Code My Repo
Example code where I tested the issue in a Kotlin project
funmain(args:Array<String>) {
val store:BoxStore=MyObjectBox.builder().name("objectbox-notes-db").build()
val imageBox = store.boxFor<Image>().also { it.removeAll() }
val propertyBox = store.boxFor<Property>().also { it.removeAll() }
val originalImage =Image()
imageBox.put(originalImage)
originalImage.apply {
properties.addAll(listOf(
Property(name ="h1"),
Property(name ="h2"),
Property(name ="h3")
))
}
imageBox.put(originalImage)
println("Boxed Properties: "+ propertyBox.all.map { it.id })
println("Original Image Properties: "+ originalImage.properties.map { it.id })
val removedID = propertyBox.all.first().id
println("Removing id ${removedID}")
val image = imageBox.get(originalImage.id).also {
it.properties.removeById(removedID) //changing the order of these 2 will make
propertyBox.remove(propertyBox.all.first().id) // it work as expected
it.properties.applyChangesToDb()
}
println("--------------")
println("Both houldn't have ${removedID}: ")
println("Boxed Properties before put: "+ propertyBox.all.map { it.id })
println("New Image Properties before put: "+ image.properties.map { it.id })
imageBox.put(image)
println("--------------")
println("Should still not have ${removedID}")
println("Boxed Properties after put: "+ propertyBox.all.map { it.id })
println("New Image Properties after put: "+ image.properties.map { it.id })
}
@Entity
data classImage(
@Id
varid:Long = 0,
....
) {
@Backlink(to ="image")
lateinitvar properties:ToMany<Property>
}
@Entity
data classProperty(
@Id
varid:Long = 0,
..
) {
lateinitvar image:ToOne<Image>
}
Additional context
Flipping the order (e.g. first removing the entity from the box) and THEN removing it from the ToMany works.
The text was updated successfully, but these errors were encountered:
it.properties.removeById(removedID)
it.properties.applyChangesToDb() // Update the relation first.
propertyBox.remove(removedID) // Then remove any objects.
The ToManyproperties is not an actual relation, but based on the ToOneimage (@Backlink). Therefore applyChangesToDb() has to modify the ToOne of a related Property to add or remove it from the relation.
It does this by setting the ToOne target, in case of removal to null, and then putting the Property.
If the Property to be updated was removed from its box in the meantime, it will not be updated, but (re-)inserted instead.
We'll see if this can be improved by explicitly requiring an update which would do nothing if the object was already removed from its box.
Side-note: you may want to have a look at ToMany.setRemoveFromTargetBox(true) which also removes related objects when they are removed from the relation.
greenrobot-team
changed the title
Entities in ToMany reappearing after removing
Entities in Backlink ToMany reappearing after removing
Apr 4, 2022
Describe the bug
When trying to remove entities from a One-To-Many relationship, they keep reappearing in the database after put() when first removing from the relationship and afterwards from box
Basic info (please complete the following information):
To Reproduce
Steps to reproduce the behavior:
one
andmany
)many
) of the other (one
).one.many.removeById(id)
box.remove(id)
one
) in the box again (overriding it)Expected behavior
No matter the order, if an entity gets deleted from the box and the relation and is nowhere to be found, it should not reappear in the box.
Code
My Repo
Example code where I tested the issue in a Kotlin project
Additional context
Flipping the order (e.g. first removing the entity from the box) and THEN removing it from the ToMany works.
The text was updated successfully, but these errors were encountered: