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

Add support for Weak/Strong links aka. Cascading Deletes

Open cmelchior opened this issue 8 years ago • 11 comments

Closes #1104

This PR adds support for the Core concepts of Weak/Strong links or effectively Cascading deletes.

The semantics are:

  • Default relationship are "weak"
  • If A has a strong relationship with B, then B is automatically deleted if A removes it's reference or is deleted itself.
  • Multiple A's can hold a strong reference to B. B is only deleted when the final A is.

In particular, this solves the most common cascading delete problems we have seen:

  • Nested hierarchies of objects where parents own their children.
  • Updating list problem: I.e. how to delete objects no longer part of a list after the list has been modified due to a network request.

The public API is a new annotation:

// Model classes
@StrongRelationship
public Person person;

@StrongRelationship
public RealmList<Dog> dogs;

// DynamicRealmObject
boolean DynamicRealmObject.isStrongRelationship("linkField");

// RealmObjectSchema
boolean RealmObjectSchema.isStrongRelationship(String fieldName);
RealmObjectSchema.setStrongRelationship(String fieldName, boolean isStrongRelationship);

API Thoughts: Cycled through a few ideas before ending on a single @StrongRelationship. This mimics the current behavior of e.g @Required where we only annotate things if different from default behavior. It restricts the number of "types" of relationships we can support, but I don't think that is a problem for the foreseeable future.

Other things attempted:

// More descriptive, can also set it explicitly to weak
@Relationship(RelationshipType.STRONG) 

// Reducing the number of annotations by adding it to other annotation proposed in PR about naming
@RealmField(relationship = Relationship.STRONG)

Should we add a @WeakRelationship? Nice for symmetry ala @Nullable/@NonNull but our own API does not have the precedence.

TODO:

  • [ ] Flesh out annotation processor unit tests
  • [ ] Add DynamicRealm API + tests
  • [ ] Add Migration API + tests
  • [ ] Merge Sync upgrade from master

cmelchior avatar Jan 12 '18 11:01 cmelchior

Oooo this is great 😄 I wonder if this will be in 5.0 along with all the other goodies

Zhuinden avatar Jan 23 '18 16:01 Zhuinden

Where can we find what release this will be in or what will be in the next release?

MarkOSullivan94 avatar Jan 24 '18 09:01 MarkOSullivan94

¯\(ツ)

Zhuinden avatar Jun 11 '18 10:06 Zhuinden

Sorry. This was put on hold due as we discovered we needed a few more internal changes before it made sense to release this. I don't have a new timeline unfortunately 😢

cmelchior avatar Jun 11 '18 10:06 cmelchior

Oh I see now :(

https://github.com/realm/realm-object-store/pull/622#issuecomment-385618340

Zhuinden avatar Jun 16 '18 23:06 Zhuinden

Realm 5.8.0. - not found cascade delete. When you implement cascade delete in Realm? Thanks.

alexei-28 avatar Nov 27 '18 14:11 alexei-28

Hi, we are using realm for database. We have created Realm classes as per our application database design. In this, There are some models which are having inter related dependancy. But they won't have primary and foreign key relation.

They add row as per defined with db structure but when deletion comes we need to manage that for every model. does realm provides cascasde deletion ?

ChinmayAcharya28 avatar Jun 17 '19 08:06 ChinmayAcharya28

disclaimer: I am not a member of Realm

They add row as per defined with db structure but when deletion comes we need to manage that for every model.

yep

does realm provides cascasde deletion ?

nope

Zhuinden avatar Jun 17 '19 15:06 Zhuinden

According to @roberhofer, this is coming. See https://github.com/realm/realm-java/issues/761#issuecomment-499296327.

ashughes avatar Jun 17 '19 23:06 ashughes

any progress?

rovkinmax avatar May 21 '20 06:05 rovkinmax

No progress on that yet. We will however release "Embedded Objects" very soon. They will automatically "cascade delete". That will however not cover all needs.

bmunkholm avatar May 21 '20 08:05 bmunkholm