I have a pre-production Swift app that is using Realm. It has a class named Product with the primary key set as "upc". I modified the class to add some additional variables and then tried to run migration. Now when I try to run the app I am getting the following error:
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Primary key property 'Product.upc' has duplicate values after migration."
I understand that I cannot have duplicate key values but it's not clear how I get around this without modifying the upc values to some new value which is not acceptable. Is there some other way to just migrate, reset or, delete the schema to get around this?
I have tried to remove the realm file and start over but I am missing something as this just reoccurs after I start it all up again.
Product class:
@objc dynamic var upc: Int = 0
@objc dynamic var itemDescription: String = ""
@objc dynamic var originalQty: Int = 0
//@objc dynamic var totalOriginalCost: String
//@objc dynamic var totalOriginalRetail: String
//@objc dynamic var vendorStyle: String
@objc dynamic var color: String = ""
@objc dynamic var size: String = ""
//@objc dynamic var clientCost: String
//@objc dynamic var totalClientCost: String
//@objc dynamic var division: String
//@objc dynamic var departmentName: String
//@objc dynamic var vendorName: String
@objc dynamic var image: String = ""
@objc dynamic var imageSlot1: String = ""
@objc dynamic var imageSlot2: String = ""
@objc dynamic var imageSlot3: String = ""
@objc dynamic var imageSlot4: String = ""
@objc dynamic var imageSlot5: String = ""
@objc dynamic var imageSlot6: String = ""
@objc dynamic var imageSlot7: String = ""
@objc dynamic var imageSlot8: String = ""
@objc dynamic var imageSlot9: String = ""
@objc dynamic var imageSlot10: String = ""
@objc dynamic var imageSlot11: String = ""
@objc dynamic var imageSlot12: String = ""
@objc dynamic var imageSlot13: String = ""
@objc dynamic var imageSlot14: String = ""
@objc dynamic var imageSlot15: String = ""
@objc dynamic var price: Double = 0
@objc dynamic var shipping: Double = 0
@objc dynamic var brand: String = ""
@objc dynamic var style: String = ""
@objc dynamic var sleeveStyle: String = ""
@objc dynamic var sleeveLength: String = ""
override static func primaryKey() -> String? {
return "upc"
}
}
migration code:
// bump the schema version to 1
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
migration.enumerateObjects(ofType: Product.className()) { oldObject, newObject in
if (oldSchemaVersion < 1) {
}
}
})
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Primary key property 'Product.upc' has duplicate values after migration."
Update: I tried the following code to reset the schema but then I get new errors:
var config = Realm.Configuration() config.deleteRealmIfMigrationNeeded = true
error: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'Product.upc' has been made required. - Property 'Product.itemDescription' has been made required. - Property 'Product.originalQty' has been made required. - Property 'Product.color' has been made required. - Property 'Product.size' has been made required. - Property 'Product.image' has been made required. - Property 'Product.imageSlot1' has been made required. - Property 'Product.imageSlot2' has been made required. - Property 'Product.imageSlot3' has been made required. - Property 'Product.imageSlot10' has been added. - Property 'Product.imageSlot5' has been added. - Property 'Product.imageSlot6' has been made required. - Property 'Product.imageSlot7' has been made required. - Property 'Product.imageSlot8' has been made required. - Property 'Product.imageSlot9' has been made required. - Property 'Product.imageSlot4' has been added. - Property 'Product.imageSlot11' has been made required. - Property 'Product.imageSlot12' has been made required. - Property 'Product.imageSlot13' has been made required. - Property 'Product.imageSlot14' has been made required. - Property 'Product.imageSlot15' has been made required. - Property 'Product.price' has been made required. - Property 'Product.shipping' has been made required. - Property 'Product.brand' has been made required. - Property 'Product.style' has been made required. - Property 'Product.sleeveStyle' has been made required. - Property 'Product.sleeveLength' has been made required.
Remove all data in your database then update schema Add, update filed after update your database must set new version
Code for update version
Code for remove all data