realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Compound primary keys

Open cmelchior opened this issue 9 years ago • 51 comments

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

cmelchior avatar May 17 '15 18:05 cmelchior

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?

senorcris avatar Jul 14 '15 20:07 senorcris

+1

shawnwall avatar Aug 12 '15 14:08 shawnwall

How can I check status of this task?

ghost avatar Sep 15 '15 14:09 ghost

+1

carmas123 avatar Sep 29 '15 06:09 carmas123

+1

hunain-liaquat avatar Jan 22 '16 13:01 hunain-liaquat

This would solve a problem I'm currently facing. Would be a great feature to have!

saket avatar Jan 23 '16 06:01 saket

It's been 6 months. What's status?

dongseok0 avatar Feb 02 '16 04:02 dongseok0

+1

yuwu avatar Feb 03 '16 14:02 yuwu

+1

willf80 avatar Mar 05 '16 15:03 willf80

+1

ohcrfpv avatar Mar 13 '16 16:03 ohcrfpv

+1

yuwu avatar Mar 15 '16 01:03 yuwu

+1

pratamawijaya avatar Mar 15 '16 02:03 pratamawijaya

+1

mksengun avatar Mar 15 '16 13:03 mksengun

+1

marchy avatar Apr 05 '16 18:04 marchy

+1

mmartikainen avatar Apr 06 '16 22:04 mmartikainen

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.

marchy avatar Apr 06 '16 23:04 marchy

+1

wuwu2000 avatar Apr 22 '16 13:04 wuwu2000

+1

razelsoco avatar May 12 '16 04:05 razelsoco

+1

mgdiez avatar Jul 25 '16 07:07 mgdiez

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 

halfred888 avatar Aug 05 '16 09:08 halfred888

@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)"

marchy avatar Aug 05 '16 13:08 marchy

+1

StefanoGR avatar Aug 26 '16 13:08 StefanoGR

+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.

tbeech avatar Aug 31 '16 00:08 tbeech

+1

dlleixap avatar Oct 07 '16 15:10 dlleixap

+1

kojadin avatar Nov 07 '16 10:11 kojadin

+1

cgathergood avatar Nov 09 '16 12:11 cgathergood

@tbeech no one is stopping you from also adding @Index annotated fields for parts of the handmade-composite primary key....

Zhuinden avatar Nov 09 '16 12:11 Zhuinden

+1

anguyenqd avatar Dec 01 '16 18:12 anguyenqd

Guys, seriously, you can just concatenate the values together into a String.

Zhuinden avatar Jan 16 '17 11:01 Zhuinden

Or you know, instead of writing +1 just click that "Subscribe" button.

Dragas avatar Jan 16 '17 12:01 Dragas