RLMException: 'Primary key property 'serial' does not exist on object 'Book' Migrating to Swift 4

Although it doesn't necessarily about migration, there's an issue with iOS 13 and Xcode 11 which may cause this problem. All String properties of Realm classes with a default String value set are disregarded somehow. You can fix this by updating to the latest version (currently 3.20.0) and than on Xcode: Product -> Clean Build Folder.

If you're using cocoa-pods, do this:

Open your project's Podfile, and replace RealmSwift line with:

pod 'RealmSwift', '~> 3.20.0'

Then, open terminal on the project's folder and:

pod repo update
pod install

Hope that helps.


In Realm, the properties of your model have to have the @objc dynamic var attribute, that is what I was missing.

From Realm website:

Realm model properties must have the @objc dynamic var attribute to become accessors for the underlying database data. Note that if the class is declared as @objcMembers (Swift 4 or later), the individual properties can just be declared as dynamic var.


import Foundation
import RealmSwift

class Book: Object {
   @objc dynamic var id : Int = 0
   @objc dynamic var serial: String = ""
   @objc dynamic var title: String = ""
   @objc dynamic var pages: Int = 0
   @objc dynamic var genre: String = ""

    override static func primaryKey() -> String? {
        return "id"
    }
}

Tags:

Ios

Swift

Realm