# `data.expected_version_unversioned`

A write says `expected_version:` of an entity that keeps no `RecordVersion`.

Area 6, [Reads and writes](/docs/reads-and-writes.md); not declinable; the next step is usually the agent's.

## Why Jadpo refuses it

`expected_version:` lets a change go ahead only while the stored record
is at the version the client read, so two changes made from one copy
can't both win. That needs a version on every record: a field typed
`RecordVersion`, which Jadpo sets to 1 on create and moves on with every
change. An entity without one has nothing to compare, and the write
would go ahead whatever the client saw.

## What it looks like

main.jadpo:

```jadpo
type TodoTitle = Text {
    max_length: 200
}

entity Todo {
    id: Uuid
    title: TodoTitle
}

type RenameTodo = Input {
    title: Todo.title
    version: Int
}

action rename_todo(todo_id: Todo.id, input: RenameTodo) fails TodoNotFound -> Todo {
    return attempt Todo.update(todo_id) {
        expected_version: input.version
        set: {
            title: input.title
        }
    }
}
```

```text
error: `Todo` keeps no version for `Todo.update` to expect
 --> main.jadpo:17:9
   |
17 |         expected_version: input.version
   |         ^^^^^^^^^^^^^^^^ no `RecordVersion`
  reason: `expected_version:` lets the change go ahead only while the stored version is the one the client read, so its entity keeps one: a field typed `RecordVersion`, which Jadpo sets to 1 on create and moves on with every change. `Todo` has none.
  next: Add `version: RecordVersion` to `Todo` and send it out and back, or remove `expected_version: ...` (agent)
  rule: data.expected_version_unversioned
  help: https://jadpo.dev/docs/errors/data.expected_version_unversioned

check failed: 1 error, 1 file
```

`(agent)` at the end of the `next:` line means your coding agent can make
the fix itself.

## How to fix it

**If people change todos from a copy they read earlier**, give `Todo` a
version, send it out with the record and back with the change, and map
a stale one in `conflict:`:

```jadpo
entity Todo {
    id: Uuid
    title: TodoTitle
    version: RecordVersion
}

type RenameTodo = Input {
    title: Todo.title
    version: Todo.version
}

action rename_todo(todo_id: Todo.id, input: RenameTodo) fails TodoNotFound, TodoChanged -> Todo {
    return attempt Todo.update(todo_id) {
        expected_version: input.version
        set: {
            title: input.title
        }
        conflict: {
            Todo.version: TodoChanged
        }
    }
}
```


A rename made from a copy someone else changed since is now refused as
`TodoChanged`, a 409, and the title they saved is kept.

**If the last change should win**, remove `expected_version: ...`:

```jadpo
action rename_todo(todo_id: Todo.id, input: RenameTodo) fails TodoNotFound -> Todo {
    return attempt Todo.update(todo_id) {
        set: {
            title: input.title
        }
    }
}
```


The rename now writes whatever version the todo holds, and the input
sends no version.

## Related

- [A change made from an old copy](/docs/when-a-write-is-refused.md#a-change-made-from-an-old-copy)
- [When a write is refused](/docs/when-a-write-is-refused.md)
- [`data.record_version_invalid`](/docs/errors/data.record_version_invalid.md)
- [`data.conflict_unmapped`](/docs/errors/data.conflict_unmapped.md)
