# `data.total_invalid`

A field's `total:` isn't a count or a sum of another entity's records that refer to its own, kept in the same domain and database.

Area 5, [Entities, stores and migrations](/docs/data.md); not declinable; the next step is usually the agent's.

## Why Jadpo refuses it

A total is kept by the database, in the same statement as each write to
the records it counts, so it counts the records of another entity that
refer to its own record, through a reference typed `<Entity>.id`, kept
in the same database and domain. A condition on those records is
written as a lifecycle's `when:` is, tests only their own stored fields,
never the time, and is one SQL tests exactly as Jadpo does. A total is
never set by any write, so it takes no setting a write would mean, and
a total with a bound reads no field erasure, retention or a deleted
reference clears, since clearing one could take it past its bound with
nothing to refuse.

Here a story counts its cheers through the cheer's reference to its
reader, which doesn't name the story.

## What it looks like

main.jadpo:

```jadpo
type CheerCount = Int {
    min: 0
}

entity Reader {
    id: Uuid
}

entity Story {
    id: Uuid
    cheers: CheerCount {
        total: count(Cheer.reader_id)
    }
}

entity Cheer {
    id: Uuid
    story_id: Story.id {
        on_delete: OnDelete.cascade
    }
    reader_id: Reader.id {
        on_delete: OnDelete.cascade
    }
}
```

```text
error: `Cheer.reader_id` doesn't refer to `Story`
 --> main.jadpo:12:22
   |
12 |         total: count(Cheer.reader_id)
   |                      ^^^^^^^^^^^^^^^ refers to `Reader`, not to `Story`
  reason: `Story.cheers` counts the records that refer to its own record, so it names a reference to `Story`, a field typed `Story.id`; `Cheer.reader_id` refers to `Reader`.
  next: Name a reference of `Cheer` typed `Story.id` (agent)
  rule: data.total_invalid
  help: https://jadpo.dev/docs/errors/data.total_invalid

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 the story counts the cheers given to it**, count them through the
cheer's reference to the story. `main.jadpo` becomes:

```jadpo
type CheerCount = Int {
    min: 0
}

entity Reader {
    id: Uuid
}

entity Story {
    id: Uuid
    cheers: CheerCount {
        total: count(Cheer.story_id)
    }
}

entity Cheer {
    id: Uuid
    story_id: Story.id {
        on_delete: OnDelete.cascade
    }
    reader_id: Reader.id {
        on_delete: OnDelete.cascade
    }
}
```

Each story now holds how many cheers name it, kept as cheers are added,
moved and removed.

**If the reader counts the cheers they gave**, keep the total on
`Reader` instead. `main.jadpo` becomes:

```jadpo
type CheerCount = Int {
    min: 0
}

entity Reader {
    id: Uuid
    cheers_given: CheerCount {
        total: count(Cheer.reader_id)
    }
}

entity Story {
    id: Uuid
}

entity Cheer {
    id: Uuid
    story_id: Story.id {
        on_delete: OnDelete.cascade
    }
    reader_id: Reader.id {
        on_delete: OnDelete.cascade
    }
}
```

Each reader now holds how many cheers they gave. A count kept apart from
what it counts, in another database or domain, or on an entity in Redis,
is an event's and a subscriber's counter instead.

## Related

- [Totals the runtime keeps](/docs/fields-and-their-settings.md#totals-the-runtime-keeps)
- [`data.total_type`](/docs/errors/data.total_type.md)
