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

Deleting Orphaned Objects during Realm Migration #7139

Closed
saltyskip opened this issue Feb 24, 2021 · 3 comments
Closed

Deleting Orphaned Objects during Realm Migration #7139

saltyskip opened this issue Feb 24, 2021 · 3 comments

Comments

@saltyskip
Copy link

saltyskip commented Feb 24, 2021

Goals

I want to migrate existing data base stores to embedded. There maybe some objects that have been unintentionally orphaned due to developer error. I want to migrate the objects to embedded and either ignore or delete orphaned objects in the process

Expected Results

Successful migration, if there are any objects that do not have a parent object they should be deleted.

Actual Results

Fatal error: Realm has crashed At least one object does not have a backlink (data would get lost).: file Bitcoin_com/AppDelegate.swift, line 98

Steps for others to Reproduce

Create several parent child relationships. Create an orphaned child object

Parent A -> Child B
Child C

When you run the migration you will get the error above due to the existance of child C. In migration block I want to delete child C if that is the case.

Code Sample

Version of Realm and Tooling

Realm framework version: Realm 10.7.0

Xcode version: 12.4

iOS/OSX version: 14.4

@DominicFrei
Copy link
Contributor

@saltyskip This change came with realm/realm-core#4414 in core. Please also refer to the discussion (and all the linked issues) in there.

TL;DR
There was a bug where users could not migrate from Object to EmbeddedObject due to missing or multiple backlinks.
One of the decisions that had to be made during that change was the handling of orphaned objects. There were two possible solutions:
(1) Silently delete objects that would now be orphaned after the switch to EmbeddedObject without the developer noticing that data gets lost.
(2) Throw an error to warn the developer about orphaned objects that would otherwise be lost after that switch.

We chose to go the safer path of option (2).

When migrating to an EmbeddedObject you have to make sure that at the end of the migration (which is when the actual change happens) every object has one and only one backlink.

@saltyskip
Copy link
Author

Hi I see, I've updated my codes to manually remove the orphaned objects, it might be nice to either have a configuration setting to delete orphans automatically or update documentation examples of migrations to show how to safely delete orphaned objects during migration.

I think its safe to delete this issue. Thanks @DominicFrei

@DominicFrei
Copy link
Contributor

Thanks for letting us know, @saltyskip.

I did create a ticket to include a helper function to make deleting those orphaned objects easier: #7145

I've also noted that an update of the documentation would be a good idea. 👍

@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

No branches or pull requests

2 participants