# Write a table's rows (/build/write-rows)

Add, change and delete a table's rows in one batch, change a row only if it still holds what you read, keep a long text out of a row, and take a batch back.



Every write to a [table](/build/tables) is a batch: a list of operations on one table of one item, applied in order, **all of them or none.** The examples write a trip budget, a `core.table` item whose one table is named `table`. In the TypeScript examples, `marfa` is a client made as [the TypeScript SDK](/reference/typescript-sdk#create-a-client) shows.

## Write a batch [#write-a-batch]

[Write table rows](/reference/api/tables/writeTableRows) takes up to 1,000 operations. This adds one row and changes a column of another:



**CLI**

```bash  
marfa rows write <id> table --body '{"operations":[
  {"op":"put","values":{"day":"2026-05-03","item":"Museum","amount":18}},
  {"op":"patch","id":"<row>","values":{"amount":52}}]}'
```

**HTTP**

```http
POST /items/<id>/tables/table/rows
Authorization: Bearer <key>
Content-Type: application/json

{ "operations": [
  { "op": "put", "values": { "day": "2026-05-03", "item": "Museum", "amount": 18 } },
  { "op": "patch", "id": "<row>", "values": { "amount": 52 } }
] }
```

**TypeScript**

```ts
const { data, error } = await marfa.POST("/items/{id}/tables/{table}/rows", {
  params: { path: { id, table: "table" } },
  body: { operations: [
    { op: "put", values: { day: "2026-05-03", item: "Museum", amount: 18 } },
    { op: "patch", id: row, values: { amount: 52 } },
  ] },
});
```

* **`put`** creates a row, or replaces every column of the row its `id` names.
* **`patch`** sets the columns it names and keeps the rest. `null` clears a column.
* **`delete`** removes a row. A row that is already gone is answered `absent`.

The answer names the `batch` and, in `results`, each row's `id` and `outcome`. `marfa rows put` and `rows patch` each send a batch of one operation, and `rows delete` a batch of deletes.

## Change a row only if it holds what you read [#change-a-row-only-if-it-holds-what-you-read]

A row has no version, so a `patch` overwrites whatever another writer left. **To write only over the values you read, name them in `expect`,** on a `patch` or a `delete`:

```json
{ "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:

Response (409 Conflict):

```json
{ "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.** `null` expects a column with no value.
* **Numbers compare by value,** so `1` matches a stored `1.0`.
* **Each `expect` is compared with the row as the operations before it in the batch left it,** and is never stored.
* **A `put` takes no `expect`.** It is refused `400 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. A [working copy](/build/working-copies) 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.

## Keep a long text out of a row [#keep-a-long-text-out-of-a-row]

**A row's values, written as JSON, are at most 64 KiB together.** A write that would leave a longer row is refused `400 validation_error` naming `operations.<index>.values`. Keep a long text in a [file item](/build/files), and name that item in a column of type `item`.

A [conversation](/catalog/conversation) does this for a long message. The whole text goes in a `core.file` that the message's `attachment` names, and `text` holds as much of the start as the row has room for. An app that finds an `attachment` of a `text/plain` file longer than `text` reads the rest from the file.

## Retry a batch [#retry-a-batch]

Send an `Idempotency-Key` header, or `--idempotency-key`, with a batch you may send twice. **A repeat under the same key is answered with the first answer and changes no row,** as [for an item](/build/write-safely#retry-with-an-idempotency-key).

## Take a batch back [#take-a-batch-back]

[Undo a table batch](/reference/api/tables/undoTableBatch) puts every row a batch changed back as it was, as a batch of its own:

```bash
marfa rows undo <id> table <batch>
```

`marfa rows batches <id> table` lists the batches the table's change log holds, newest first. An undo is refused `409 undo_conflict` when a row stands neither as the batch left it nor as it was before, because a later batch changed it: undo the later batches first, newest first. Undo an undo to redo it.

The rules are in the contract's [tables](https://github.com/withmarfa/marfa/blob/main/conformance/spec/tables.md#writing-rows) chapter.
