- Postgres and MySQL: use a UUID for
id
. Use theuuid()
helper to generate a random UUID (v4) on the client. - MongoDB: use an
ObjectId
for_id
. Generate anObjectId()
in your app code and store it in the client’sid
column as a string; this will map to MongoDB’s_id
.
MongoDB uses
_id
as the name of the ID field in collections. Therefore, PowerSync requires using SELECT _id as id
in Sync Rule’s data queries when using MongoDB as the backend source database. When inserting new documents from the client, prefer ObjectId
values for _id
(stored in the client’s id
column).If you want to upload data to a table with a custom record ID, ensure that
uploadData()
isn’t blindly using a field named id
when handling CRUD operations. See the Sequential ID mapping tutorial for an example where the record ID is aliased to uuid
on the backend.- A non-unique column is used for the ID.
- Multiple table partitions are used (Postgres), 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).
Postgres: Strategies for Auto-Incrementing IDs
With auto-incrementing / sequential IDs (e.g.sequence
type in Postgres), the issue is that the ID can only be generated on the server, and not on the client while offline. If this must be used, there are some options, depending on the use case.
Option 1: Generate ID when server receives record
If the client does not use the ID as a reference (foreign key) elsewhere, insert any unique value on the client in theid
field, then generate a new ID when the server receives it.