Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove column in Realm

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?

like image 833
Happiehappie Avatar asked Dec 16 '15 06:12

Happiehappie


2 Answers

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.

like image 108
Lucas Chwe Avatar answered Sep 28 '22 04:09

Lucas Chwe


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.

like image 39
TiM Avatar answered Sep 28 '22 06:09

TiM