Peter Pik
Peter Pik

Reputation: 11193

Migrate primary key in Realm

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"
    }
}

Upvotes: 4

Views: 4672

Answers (3)

Leo1971
Leo1971

Reputation: 21

  1. open AppDelegate.swift
  2. add following code to the func application:

    let config = Realm.Configuration(
    
    // set a new version number, the version number must bigger than before
    
        // if you never set it, it's 0
    
        schemaVersion: 1,
    
        migrationBlock: { migration, oldSchemaVersion in
    
        if (oldSchemaVersion < 1) {
          // do nothing
          }
        })
    
        // tell Realm the new config should be used 
    
        Realm.Configuration.defaultConfiguration = config
    
        // open realm file and it will do auto-migration
    
        let realm = Realm()
    

Upvotes: 2

Pedro Paulo Amorim
Pedro Paulo Amorim

Reputation: 1949

Update: On Realm 2.8.3, the property primaryKeyProperty is not necessary anymore for the migration.

I can't edit the ReCamilio answer, sorry.

Upvotes: 1

recamilio
recamilio

Reputation: 89

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!

Upvotes: 7

Related Questions