realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Handle two keys #7815

Closed KunNiu closed 1 year ago

KunNiu commented 1 year ago

Problem

Say I have two fields String arg1 & String arg2, Both arg1 and arg2 may be duplicated, only arg1+arg2 is unique. How to handle this case? I can add a field String arg3 = arg1 + arg2, and set it as primary key. But it's complex and may case problems. Like get the values from server(json), and convert to module then save. If a group member don't know or forget to set the value of arg3, then the problem happen.

Solution

No response

Alternatives

No response

How important is this improvement for you?

I would like to have it but have a workaround

Feature would mainly be used with

Local Database only

rorbech commented 1 year ago

Hi @KunNiu. Unfortunately we don't have a option to define a cross-field primary key. The obvious workaround is to do accessors for transient arg1 and arg2 that reads/updates a combined field with some kind of separator, but I guess it depends a bit on the other requirements you have if it suits your use case; is arg3 intended to be updated and reflected in arg1 and arg2 or always just derived from the two, does arg3 have a meaning on its own or only used to enforce the individually or always.

We have an issue https://github.com/realm/realm-java/issues/1129 that tracks the specific feature, so I will close this as a duplicate and you could maybe go through some of the comments of the issue to see if some suggestions fit you use cases better.

KunNiu commented 1 year ago

Thank you @rorbech , I will keep eyes on the issue you mentioned.