realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.33k stars 2.15k forks source link

Cycles containing embedded objects support #7856

Open KamalElShazly opened 2 years ago

KamalElShazly commented 2 years ago

Problem

I'm trying to make something that looks like the following:

class Parent: Object { @Persisted var children: List }

class Child: EmbeddedObject { @Persisted var children: List }

I get the following error: Schema validation failed due to the following errors: Cycles containing embedded objects are not currently supported: 'Child.children'

I'm using EmbeddedObject because I need children to be deleted when their parent is deleted.

Solution

Add support for Cycles containing embedded objects

How important is this improvement for you?

Would be a major improvement

ejm01 commented 2 years ago

I'm using EmbeddedObject because I need children to be deleted when their parent is deleted.

This can be done by

class Parent: Object {
@persisted var children: List<Child>
}

Since Child is an embedded object type they can't exist independently. If the parent is deleted then the child would be also.

The error refers to this part:

class Child: EmbeddedObject {
@persisted var children: List
}

Where the embedded object class embeds its own type, which isn't supported.

KamalElShazly commented 2 years ago

@ericjordanmossman but I need each child to have a list of children, that's why am asking to support cycles in Embedded Object.

My real life scenario is that i have a Form, that form have children. Chlidren type might be a question or a section. Each section have children (might be sections or questions also) and so on. Thats why i have a multi level of children.

dianaafanador3 commented 2 years ago

Hi @KamalElShazly unfortunately there are some limitation server side which restricts the use of nested embedded objects, so this may not be possible in the near future. As you may already know, the best solution is to use Object instead of EmbeddedObjects and use cascading delete for any Parent delete.

marcofuentes05 commented 1 year ago

Is there any way this issue can be bumped? In my case, I'm following the docs and now I get this error. If this is not possible because of some server side limitations, could you guys please update the docs?