top | item 38485230

(no title)

ricg | 2 years ago

How do you handle primary keys? Wouldn't auto-incrementing integers lead to duplicate keys since clients are not aware of each other?

For the To Do app example, let's say two clients start with an empty database and each creates a new todo:

Client A creates [id:1, todo:"Buy milk"]

Client B creates [id:1, todo:"Buy cheese"]

discuss

order

prawnstar|2 years ago

Hi ricg thanks for the great question! We have a docs page that speaks to this https://docs.powersync.com/usage/sync-rules/client-id:

"PowerSync does not perform any validation that IDs are unique. Duplicate IDs on a client could occur in any of these scenarios:

- A non-unique column is used for the ID.

- Multiple table partitions are used, with the same ID present in different partitions.

- Multiple data queries returning the same record. This is typically not an issue if the queries return the same values (same transformations used in each query)."

matharmin|2 years ago

To expand on what prawnstar said - it's exactly because of this that we recommend using uuids as the primary key (uuid v4 specifically). While you can use integer ids, you need some workarounds such as pre-generating sets of unique ids per device.