Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composite primary key realm/swift

Tags:

ios

swift

realm

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 ?

like image 771
halfred Avatar asked Jul 07 '15 10:07

halfred


People also ask

What is composite primary key?

A Composite Primary Key is created by combining two or more columns in a table that can be used to uniquely identify each row in the table when the columns are combined, but it does not guarantee uniqueness when taken individually, or it can also be understood as a primary key created by combining two or more ...

What is the difference between composite and primary key?

While a primary key and a composite key might do the same things, the primary key will consist of one column, where the composite key will consist of two or more columns. The relationship between a primary key and a foreign key is quite different.

What is composite primary key with example?

Example of Composite Key Consider table A having three columns or attributes, which are as follows: Cust_Id: A customer id is provided to each customer who visits and is stored in this field. Order_Id: Each order placed by the customer is given an order id, which is stored in this field.

What is realm for Swift?

Realm Swift is an easy to use alternative to SQLite and Core Data that makes persisting, querying, and syncing data as simple as working directly with native Swift objects. Deploy a sample app View documentation.


2 Answers

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.

like image 182
Michal Avatar answered Sep 24 '22 01:09

Michal


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)" 
like image 34
Rajan Twanabashu Avatar answered Sep 20 '22 01:09

Rajan Twanabashu