Change or delete a table row only as you read it
Change a row only while it holds the values you read, and delete one only while it has the version you read, so no other writer's work is lost.
Two writers can read the same row of a table. Without a check, a patch overwrites what the other writer saved since, and a delete removes it. Name what you read, and Marfa writes nothing of the batch if the row has moved on. The examples write a trip budget, a core.table item whose one table is named table.
Change a row only if it holds what you read
To write only over the values you read, name them in expect, on a patch or a delete:
{ "op": "patch", "id": "<row>", "values": { "amount": 55 }, "expect": { "amount": 48.5 } }If a column named in expect holds another value, Marfa writes nothing of the batch and answers 409 row_changed. details.operation is the operation's place in the batch, and details.row holds the values the row has now:
{ "error": { "code": "row_changed", "message": "The row no longer holds the values expect names, so nothing of the batch was written. Read the row again.",
"details": { "operation": 0, "row": { "id": "01a2394f-0f72-748b-ad84-2b309109a0c4", "values": { "amount": 52 } } } } }- Only the columns you name are compared.
nullexpects a column with no value. - Numbers compare by value, so
1matches a stored1.0. - Each
expectis compared with the row as the operations before it in the batch left it, and is never stored. - A
puttakes noexpect. It is refused400 validation_error, as is a column the table doesn't declare.
Show the person the values in details.row, read the row again, and send the write with what they choose.
Delete a row only at the version you read
Every row has a version, a whole number that each change to the row raises. Marfa never gives a version twice, so a row deleted and put again under its ID has a new one. A read answers the version beside id and updated_at. A batch answers in results the version each put or patch left its row at, and previous_version where the row was there before the batch.
Name the version you read in a delete, and it removes the row only while the row is still in that state:
{ "op": "delete", "id": "<row>", "version": 1796258166869 }If the row has another version, Marfa writes nothing of the batch and answers 409 row_changed. details.rows names each such delete by its operation, with the row's id and the version it has now:
{ "error": { "code": "row_changed", "message": "A row has changed since the version a delete names, so nothing of the batch was written. Read the row again.",
"details": { "operation": 0, "rows": [{ "operation": 0, "id": "01a2394f-0f72-748b-ad84-2b309109a0c4", "version": 1796258512118 }] } } }- The version is compared with the row as it stood before the batch, so a
patchand then adeleteof a row you read can go in one batch. - A row that is already gone is answered
absent, so a delete sent again after a lost answer is acknowledged. - A delete that names no
versionalways removes the row. - Only a
deletetakes aversion. Aputorpatchthat names one is refused400 validation_error. - A restore keeps each row's version. A row restored from an archive that 0.0.124 or earlier wrote gets a new one, so a delete that names its old version is refused.
Show the person the row as it is now, and delete it again if they still want to.
In a working copy
A working copy never sends expect: it refuses a row write that names one, and gives a write that replaced another writer's value the verdict conflicted once the server takes it.
A row the copy holds carries the version the server last gave it. A row that only a waiting write made has none. In a table the copy holds whole, a queued delete that names no version takes the one the copy holds, and an app can name a version it read itself. If the server's row has changed since, the write is kept refused with the reason row_changed, and the copy shows the server's row. Handle a write's verdict says what to do with a refused write.
The rules are in the contract's tables chapter.