Remove column in Realm
Asked Answered
P

3

7

I was checking out the migration documentation, however, I'm not sure if I overlooked or it's not stated, but I noticed that the documentation only explains how to add column, what if I want to remove a column?

Pampuch answered 16/12, 2015 at 6:57 Comment(1)
// Realm will automatically detect new properties and removed properties and will update the schema on disk automatically sourceDimity
D
6

In the Migrations section of the Realm Swift documentation, it actually shows you how to remove two columns (firstName and lastName), and replace them with a single new property (i.e. fullName).

To remove columns from your Realm file, all you need to do is remove those properties from your model object, and then run a migration.

If there's information in those columns you wish to keep, you can choose to move that information to a new property inside the migration closure (Which is what the documentation demonstrates). This is completely optional, and if you run the migration with an empty closure, then the columns will simply be removed and the data deleted.

Dodds answered 16/12, 2015 at 7:37 Comment(3)
My pleasure! Yeah, these ones can be kind of tricky; where it's such a simple thing, that you're not even sure that it's the correct way. ;) Good luck!Dodds
Thank you, I have another migration problem on the way here on stack overflow. Just waiting for the 90 minute cooldown, do check it out too if possible.Pampuch
done #34307611Pampuch
J
7

The answer is not correct because these kind of basic migrations and schema updates dont happen automatically, ie. if you only remove a property, and re run your app, you ll get a crash.

The answer is you MUST increase the value of the schemaVersion in Realm.Configuration in order to trigger the basic built-in schema updates described in the rest of the answers and realm documentation.

Jive answered 21/2, 2019 at 17:54 Comment(0)
D
6

In the Migrations section of the Realm Swift documentation, it actually shows you how to remove two columns (firstName and lastName), and replace them with a single new property (i.e. fullName).

To remove columns from your Realm file, all you need to do is remove those properties from your model object, and then run a migration.

If there's information in those columns you wish to keep, you can choose to move that information to a new property inside the migration closure (Which is what the documentation demonstrates). This is completely optional, and if you run the migration with an empty closure, then the columns will simply be removed and the data deleted.

Dodds answered 16/12, 2015 at 7:37 Comment(3)
My pleasure! Yeah, these ones can be kind of tricky; where it's such a simple thing, that you're not even sure that it's the correct way. ;) Good luck!Dodds
Thank you, I have another migration problem on the way here on stack overflow. Just waiting for the 90 minute cooldown, do check it out too if possible.Pampuch
done #34307611Pampuch
G
0

Actually, Lucas's answer is correct. I've got nil Realm instance when I deleted one column and did not change the schemaVersion value.

Debugging that piece of code showed that Realm returns an error that states you HAVE TO change the schemaVersion value in order for Realm to update DB schema. Otherwise, as we see, Realm will not return Realm object instance to you but nil.

Realm version is 10.7.2

Gaff answered 14/4, 2021 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.