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 ?
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 ...
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.
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.
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.
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.
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)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With