Reputation: 730
I am trying to do migration for data type for one of a property in my Model file.
Sources that I've found are mainly guide on how to migrate if there is a name change of the column or combine the column into one.
And here are the error message that I got when I compile the app.
Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required due to the following errors: - Property types for 'has_completed_profile' property do not match. Old type 'bool', new type 'int'
And I am using the latest version 0.99.0
Upvotes: 3
Views: 1433
Reputation: 10603
First, you should increment schema version when you change the definition of the model classes.
Then if you keep old data with new data schema, you should migrate old data to new schema within migration block.
For example:
// Schema version 0
class TestObject: Object {
dynamic var name = "Test"
dynamic var has_completed_profile = false
}
// Schema version 1
class TestObject: Object {
dynamic var name = "Test"
dynamic var has_completed_profile = 5
}
if you change column time Bool
to Int
, and you'd like to keep old data, you should write migration block, like the following:
let config = Realm.Configuration(schemaVersion: 1, migrationBlock: { (migration, oldSchemaVersion) in
if oldSchemaVersion < 1 {
migration.enumerate(TestObject.className(), { (oldObject, newObject) in
// Migrate old column to new column
// If there is no compatibility between two types
// (e.g. String to Int)
// you should also write converting the value.
newObject!["has_completed_profile"] = oldObject!["has_completed_profile"]
})
}
})
let realm = try! Realm(configuration: config)
Upvotes: 6