realm-java
realm-java copied to clipboard
Compound primary keys
Realm should also support composite primary keys. Reusing the @PrimaryKey
annotation should be relatively simple.
public class CompositeKeyObj extends RealmObject {
@PrimaryKey
private int compositePartOne;
@PrimaryKey
private int compositePartTwo;
}
Primary keys are currently implemented in the binding. This should be implemented at the core level: https://github.com/realm/realm-core/issues/831
This feature would be great is it still on the roadmap? If not are there any work arounds that could be used in the meantime to get composite keys?
+1
How can I check status of this task?
+1
+1
This would solve a problem I'm currently facing. Would be a great feature to have!
It's been 6 months. What's status?
+1
+1
+1
+1
+1
+1
+1
+1
One workaround we've used as a way of getting around this limitation is to create an 'id' property which simply concatenates the different fields that you want to use in your composite key. This way you get the uniqueness integrity checks but you can still program against the proper abstractions (ie: foreign keys/objects that make up your composite key - especially useful for "link table" objects between your various entities.
+1
+1
+1
A workaround what I have been using is to create the unique id of combining two id's before assigning it to realm object eg.
class Pet : Object {
dynamic var combinedPrimary : String = ""
override class func primaryKey() -> String? {
return "combinedPrimary"
}
}
let combinedKey = "\(person.id)\(pet.id)"
pet.combinedPrimary = combinedKey
@realpunk yup I think that's the recommended workaround. It's probably why the team hasn't been prioritizing this very much - along with the fact that Realm doesn't really expose foreign keys in any way (I'm no expert here, just my experience with it). So unlike traditional ORMs where you want to keep foreign keys around on objects to link to them in Realm you link to the object instance directly and the backing ID/structure is hidden from the programming model.
For readability/sanity purposes I'd recommend throwing an extra ":" separator in there so you can re-infer the original IDs if needed:
ie: "(person.id):(pet.id)"
+1
+1
I'm not so sure that concatenating two keys together "really" solves the problem. Generally compound PK's are used so that you can query part of the key to return a list of related items. For instance I might want to log a set of actions where the log entry pk might be log.id + log.actionId as separate columns. Concatenating the two prevents me from querying against the db such that:
NSString *logId = @"SOME UUID";
NSPredicate *query = [NSPredicate predicateWithFormat=@"%K == %@", @"id", logId];
RLMResults<LogEntry *> *results = [LogEntry objectsWithPredicate: query];
Compound PK's also go hand in hand with unique constraints which Realm currently does not support.
+1
+1
+1
@tbeech no one is stopping you from also adding @Index
annotated fields for parts of the handmade-composite primary key....
+1
Guys, seriously, you can just concatenate the values together into a String.
Or you know, instead of writing +1 just click that "Subscribe" button.