How merges work
The server and every device merge changes with the same rules, so every copy ends up with the same data. These rules tell you which edit wins, and why a value or a row may look wrong.
Which edit wins
Each column merges on its own. When two devices edit the same column, the edit with the newer timestamp wins. These details decide which edit is newer, and what counts as an edit:
- Time comes from the device that made the edit. An edit made offline keeps the time it was made, so an older edit that syncs later still loses to a newer one.
- A full-row save edits every column. Each column gets a new timestamp, even if its value didn't change. That's why your code should update only the columns that changed.
- A list, map, set, or nested model is one value. The newer edit replaces the whole value. To keep additions from several devices, use child rows, as Model synced data shows.
- Device clocks must be close. A sync fails with
ClockDriftExceptionif it brings in a change dated more than a minute in the future. A write fails the same way if the device's clock is more than a minute behind the newest change it has seen. Fix the device's clock to write and sync again.
Deletes and restores
A delete hides a row, and an insert with the same id restores it. When one device deletes a row and another edits or restores it, these rules apply:
- An edit never restores a deleted row. The edit is kept in the hidden row and shows if the row is restored.
- A restore beats the delete it undid. This holds even when the delete syncs later. Devices can delete and restore the same row several times. Then the copy that went through the most deletes and restores decides whether the row is visible.
- An insert from a device that never had the row doesn't restore it. It counts as new and loses to the delete.
Relations after a merge
On a single device, a write that breaks a relation throws, as in any database. A reference to a parent in another space is the exception. It doesn't throw, and the child is treated as if its parent were missing. A merge never leaves a child row pointing at a missing parent, because of these rules:
- The relation's
onDeleteaction decides what happens to a new child. That's a child another device adds while one device deletes the parent. See ChooseonDeletefor each relation. - A row whose parent is missing is hidden, unless its relation uses
SetNullorSetDefault. This happens when the parent belongs to another space, or is deleted or missing on this device. - A chain of deletes applies whole or not at all. If a
Cascadedelete reaches a row that can't be deleted, such as a row with aRestrictchild, every row in the chain stays visible.
Unique values after a merge
When two rows in a space set the same unique value, the row that set it first keeps it. The other row stays visible with a replacement value:
| Column | Replacement |
|---|---|
| Nullable, of any type | null |
Non-nullable String | <value>__conflict__<rowId> |
Non-nullable UuidValue without a relation | A generated version 8 UUID |
Offline sync never merges the two rows into one. If the row that holds the value is deleted or changes the value, the row that set it next gets the value back. A row that writes a new value is treated as setting it at that moment.
To let users resolve a conflict, check for the suffix and ask for a new value:
final hasConflict = RegExp(r'__conflict__[0-9a-fA-F-]{36}$').hasMatch(list.name);
When a value differs from what was written
A merge can show a value other than the one a device wrote. The written value is kept, and it comes back when the cause goes away:
| You see | Because | It changes back when |
|---|---|---|
A foreign key that reads null or its default | The parent was deleted on another device, and the relation uses SetNull or SetDefault | The parent is restored |
A __conflict__ value or another replacement | Another row set the value first | That row is deleted or changes its value |
| A missing row | Its parent is missing or hidden | The parent comes back |
Related
- Model synced data: patterns that keep edits from being lost, mixed, or duplicated.
- Deleted and hidden rows: reading, listing, and restoring hidden rows.