composite primary key realm/swift

halfred picture halfred · Jul 7, 2015 · Viewed 10.2k times · Source

I'm new to swift and realm. I want to make a composite primary key and when I'm trying something like this :

class DbLocation : Object {
 dynamic var id = 0
 dynamic var tourId = 0

 dynamic var uuid : String  {
    return "\(id)\(tourId)"
 }

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

I'm getting this error : 'Primary key property 'uuid' does not exist on object 'DbLocation'

Anyone can help me out with an example how to create a composite primary key ?

Answer

Michal picture Michal · Jul 7, 2015

For 1.0.1+ of Realm:

class DbLocation: Object{
    dynamic var id = 0
    dynamic var tourId = 0
    dynamic var compoundKey = ""

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

    func setup(id: Int, tourId: Int){
        self.id = id
        self.tourId = tourId
        self.compoundKey = compoundKeyValue()
    }

    func compoundKeyValue() -> String {
        return "\(id)\(tourId)"
    }
}

Usage example:

let location = DbLocation()
location.setup(id: 0, tourId: 1) 
print(location.compoundKey) // "01"

Of course you can play around with using various didSet listeners on id and tourId, to make sure that compoundKey gets properly rewritten every time the values get changed.

For pre-1.0.1 of Realm:

class DbLocation: Object {
    dynamic var id = 0
    dynamic var tourId = 0

    func setCompoundID(id: Int) {
        self.id = id
        compoundKey = compoundKeyValue()
    }

    func setCompoundTourId(tourId: Int) {
        self.tourId = tourId
        compoundKey = compoundKeyValue()
    }

    dynamic lazy var compoundKey: String = self.compoundKeyValue()

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

    func compoundKeyValue() -> String {
        return "\(id)\(tourId)"
    }
}

The custom setters make sure, that the compoundKey is always updated, the lazy key word makes sure that the first time you access it, it will be derived from what you've already set.

Find out more on this topic in this thread where this issue has been debated.