Update "foreign key"

15 views
Skip to first unread message

Ivan Schuetz

unread,
Mar 26, 2016, 8:33:13 PM3/26/16
to Realm
Hi,
I'm wondering how to replicate some behaviour I have in the server in an iOS app which uses Realm. In the server I have tables A and B and A has to foreign key to B, which is the uuid of B. In Realm A and B are realms and A has a B as a property. The uuid of B is used as primary key.

Now I want to sometimes update the foreign keys of A to point to a different B, which is in the server simply updating the foreign key column of A with the new uuid. In realm I'm not sure how to approach this, not sure if it's possible to do this kind of update, and if yes probably I'll be updating the uuid of the existing referenced B instead of pointing to a new B?

Do I always have to load first B, to set it in A and save it? Or is there a lighter way to do this?

Thanks,
Ivan


Ivan Schuetz

unread,
Mar 31, 2016, 7:18:15 AM3/31/16
to JP Simard, realm...@googlegroups.com
Hi,

thanks for answering! Yes, makes sense, the thing was that this is for some very frequent updates I'm sending to the app via websockets and I was wondering how to reduce the payload to the smallest possible. The object has quite a lot of fields and dependencies and didn't want to have send everything to do the update. But I found a reasonable compromise, so it's solved.

Best,
Ivan



2016-03-29 0:15 GMT+02:00 JP Simard <he...@realm.io>:
Hi Ivan, this is a fairly open ended design question, but I'll do my best to answer it.

Typically, foreign & primary keys are used to model relationships in a pseudo object graph in relational databases that don't have native object links like Realm does. Because Realm is an object graph database which natively and efficiently represents links between objects, you're best to convert your foreign & primary keys into to-one, to-many and inverse relationships in your Realm models. You can read more about relationship modeling techniques in Realm in the Models/Relationships section of our documentation.

This way, when "updating the foreign keys of A to point to a different B" in your relational database, you could simply update the linked-to value in object A in Realm. This should be fairly similar to how you would model this using pointers and language-level Swift or Objective-C classes and structs. This is part of the design we've aimed for when defining the Realm APIs.

If you'd like us to clarify any of these points, please let us know. If you have some sample data you'd like to share that represents your current relational schema and would like us to assist you in designing an equivalent object graph schema, we're happy to do that too.

Best,
JP

--
JP Simard

{#HS:185750878-3524#}
--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/5d29b2fb-d3dd-4923-b3d9-3760ffe4ab26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.




Reply all
Reply to author
Forward
0 new messages