ErrorsEntities, stores and migrations · rule
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; 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:
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 }}
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.cheerscounts the records that refer to its own record, so it names a reference toStory, a field typedStory.id;Cheer.reader_idrefers toReader.- next
- Name a reference of
CheertypedStory.id(agent) - rule
- data.
total_ invalid - help
- https://jadpo.
dev/docs/errors/data. total_ invalid
(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:
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:
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
Markdown for agents: /docs/errors/data.total_invalid.md.Built from Jadpo's main branch at commit 72d483f.