objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Support delete cascade

Open linheimx opened this issue 7 years ago • 14 comments

it's very helpful. greendao doesn't support it, hope it in here!

linheimx avatar Apr 27 '17 17:04 linheimx

Can you specify what kind of cascade you are looking for? E.g. delete cascade?

greenrobot avatar Apr 28 '17 17:04 greenrobot

yes,delete cascade

linheimx avatar Apr 29 '17 00:04 linheimx

Delete cascades is a dangerous thing to do, especially if you do this recursively. ("whoops where is my data??") Let's see.

greenrobot avatar Apr 29 '17 09:04 greenrobot

Reading through the docs on 1.0.1, a first step might be to clarify exactly what behaviors to expect when you delete while using ToOne and ToMany.

Maybe a brief blurb here, where you discuss removal of objects. http://objectbox.io/documentation/introduction/

Another place might be the javadocs of ToOne. Does getTargetId on an "orphaned" referent return an invalid value? No idea, honestly.

The typical behaviors would be set-to-null, throw exception or cascade delete I guess. I suppose this may be obvious to those expecting a NOSQL db, but coming from relational and realm I can't figure out the answer from current docs.

bensandee avatar Sep 19 '17 23:09 bensandee

Thanks for the feedback. We will soon clarify that ToOnes are "nulled out" when the target entity is removed (target ID becomes 0).

greenrobot avatar Sep 20 '17 13:09 greenrobot

I propose to add an annotation for cascading delete that can be put on any possible relation in the entity.

For example:

class AEntity { @CascadeDeletion var bEntities : ToMany<BEntitiy> }

When i remove the AEntitiy then i expect that the bEntities will be removed too if no other AEntity that points on it anymore(ofcourse that you can leave this choice for the developer).

vadimh77 avatar Feb 07 '18 12:02 vadimh77

I agree with @vadimh77 - adding the ability to configure the behavior per relation seems like a good way to mitigate the aforementioned "whoops where is my data?" situation for most cases while still providing the functionality for the situations where cascading behavior makes sense. Google's Room library supports configuring the behavior ( https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.html )

bryndsey avatar Apr 21 '18 17:04 bryndsey

I agree with @blinz117 and @vadimh77. I am currently evaluating migrating a full project from Realm to objectbox. AFAIK in Realm cascading deletes are the default behavior. From my point of view think this is an essential feature. What's currently the best practice to do this?

meierjan avatar Jun 16 '18 21:06 meierjan

AFAIK in Realm cascading deletes are the default behavior.

It's really not, on deletion, links to the object are removed (item is removed from RealmLists, links to object become null), but there is no (publicly available and supported) way of marking a "parent-child" relationship in which "if the parent is removed, then so is the child".

The children have to be deleted manually, and then the parent manually. Which is technically manual cascade deletion.

It almost happened in https://github.com/realm/realm-java/pull/5678 :disappointed:

Zhuinden avatar Jun 16 '18 23:06 Zhuinden

Okay, I guess I had a misconception there - glad we talked about it :)

meierjan avatar Jun 17 '18 09:06 meierjan

For me the cascading delete is a essential feature.

Whithout this we broke the concept of removing a entire collection, cause i will need to iterate over every object to get his relation for manual delete

eduvm avatar Dec 24 '18 10:12 eduvm

JPA supports it and you can configure orphan removal and stuff.

Zhuinden avatar Jan 07 '19 13:01 Zhuinden

please add this feature it will be so much use full

tapanrgohil avatar Mar 30 '19 11:03 tapanrgohil

When tis is going to support this?

khandallalit avatar Mar 15 '24 12:03 khandallalit