I've just recently added a primary key to my Realm model and therefor i keep getting below error. i've tried to migrate in appdelegate but still get the rror. all i've done is adding the propertyKey()
function. How can i migrate properly?
Migration is required for object type 'Organization' due to the following errors:
- Property 'id' has been made a primary key."
However i've allready added below to appdelegate:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// The enumerate(_:_:) method iterates
// over every Person object stored in the Realm file
migration.enumerate(Organization.className()) { oldObject, newObject in
// combine name fields into a single field
}
}
})
here is my object
class Location: Object {
var id: Int = 0
var longitude: Double = 0
var latitude: Double = 0
override class func primaryKey() -> String {
return "id"
}
}
class Organization: Object {
var id: Int = 0
var name: String = ""
var image: NSData = NSData()
let locations = List<Location>()
override class func primaryKey() -> String {
return "id"
}
}
If the Model didn't have a primary key before, you can fix it by doing so:
//MARK: Realm Migrations
Realm.Configuration.defaultConfiguration = Realm.Configuration(
// bump the schema version to 2
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in
migration.enumerate(Organization.className()) { oldObject, newObject in
// make sure to check the version accordingly
if (oldSchemaVersion < 2) {
// the magic happens here: `id` is the property you specified
// as your primary key on your Model
newObject!["primaryKeyProperty"] = "id"
}
}
}
)
I hope it helps,
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With