0

Best practise to use primary key for data synced from postgres to ROS then to app.

User can insert data to these tables and they will be

synced back to postgres.

Definitely

maxId + 1 is not the solution

as multiple users can insert at same time. so if we can use a UUID should I store the it in postgres too or is it only for ROS?

And realm doesn't allow to skip primary key while inserting (which could be handled by postgres).

Or is there any other better practise to handle two way data sync for realm & postgres

Gaudam Thiyagarajan
  • 1,022
  • 9
  • 24

1 Answers1

0

Use sequence. It will make sure each of parallel requests will have a new ID, without collisions with other users / requests / threads.

With Google you will find multiple examples. Just one: https://www.ntchosting.com/encyclopedia/databases/postgresql/sequence/

mentallurg
  • 4,967
  • 5
  • 28
  • 36
  • the question was about inserting data in realm not with respect to postgres. because the app in offline mode can insert anytime data into realm and will be synced to server. so you cannot use id at all in realm during insertion – Gaudam Thiyagarajan Jun 02 '18 at 14:28
  • 1
    OK. Then is no trivial solution. There can be many approaches. 1. As you said, use UUID. I'd use it on both sides, in Realm and in Postgres. 2. Extend your primary key so that it consists of two fields - user ID and an incremental ID within this user. Then when you go offline, it is safe to generate new IDs for this user, because there will be no interesection with other users. This would work if you have one device for the user. If many, then it will not work or will be much more complicated, then uses the 1st approach, UUID. – mentallurg Jun 02 '18 at 14:39