composite primary key realm/swift

后端 未结 3 1708
攒了一身酷
攒了一身酷 2020-12-14 06:40

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 =         


        
3条回答
  •  生来不讨喜
    2020-12-14 07:20

    Simple create a new property whose value is set to interested other properties which you expect to be compound primary keys.

    class DbLocation: Object {
                dynamic var id = 0
                dynamic var tourId = 0
                dynamic var compoundKey: String? = ""
    
            override static func primaryKey() -> String? {
                    return "compoundKey"
                }
            }
        let location = DbLocation()
        location.tourId = 1
        location.id = 5
        location.compoundKey = "\(id) \(tourId)"
    

提交回复
热议问题