Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm.io and compound primary keys

I'm using Realm for Swift 1.2, and I'm wondering how to implement a compound primary key for an entity.

So you specify your primary key by overriding primaryKey()

override static func primaryKey() -> String? { // <--- only 1 field
    return "id"
}

The only way I can see is to create another compound attribute like so

var key1 = "unique thing"
var key2 = 123012

lazy var key: String? = {
    return "\(self.key1)\(self.key2)"
}()

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

How do you properly supply compound keys in Realm?

like image 349
tyler Avatar asked Aug 17 '15 16:08

tyler


People also ask

What is a compound primary key?

A compound primary key is made up of multiple fields and follows the requirements described in Primary Keys in Entities. To use a compound primary key, you must create a wrapper class. In order, two entities use compound primary keys: Part and LineItem .

What is the difference between composite and compound key?

In database design, a composite key is a candidate key that consists of two or more attributes (table columns) that together uniquely identify an entity occurrence (table row). A compound key is a composite key for which each attribute that makes up the key is a foreign key in its own right.

How to Set primary key in Realm Swift?

On the view controller write a simple function to get the “timeIntervalSince1970″, which is a built-in function with swift. You also need to import the Realm Framework on the view controller too. And finally update the unique ID to Realm.

How to Set primary key in Realm android?

Primary key values are immutable. To change the primary key value of an object, you must delete the original object and insert a new object with a different primary key value. You cannot change the primary key field for an object type after adding any object of that type to a realm.


2 Answers

It appears that is the correct way to return a compound key in Realm.

Here's the answer from Realm : https://github.com/realm/realm-cocoa/issues/1192

You could use a mix of lazy and didSet instead to have the compoundKey property be both derived and stored:

public final class Card: Object {
    public dynamic var id = 0 {
        didSet {
            compoundKey = compoundKeyValue()
        }
    }
    public dynamic var type = "" {
        didSet {
            compoundKey = compoundKeyValue()
        }
    }
    public dynamic lazy var compoundKey: String = self.compoundKeyValue()
    public override static func primaryKey() -> String? {
        return "compoundKey"
    }

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

// Example

let card = Card()
card.id = 42
card.type = "yolo"
card.compoundKey // => "42-yolo"
like image 65
chimpymike Avatar answered Oct 18 '22 22:10

chimpymike


The correct answer needs an update (regarding didSet and lazy)

Taken from "mrackwitz" at Realm git-hub: https://github.com/realm/realm-cocoa/issues/1192 :

public final class Card: Object {
    public dynamic var id = 0
    public func setCompoundID(id: Int) {
        self.id = id
        compoundKey = compoundKeyValue()
    }
    public dynamic var type = ""
    public func setCompoundType(type: String) {
        self.type = type
        compoundKey = compoundKeyValue() 
    }
    public dynamic var compoundKey: String = "0-"
    public override static func primaryKey() -> String? {
        return "compoundKey"
    }

    private func compoundKeyValue() -> String {
        return "\(id)-\(type)"
    }
}
like image 27
Roee84 Avatar answered Oct 18 '22 21:10

Roee84