hcengineering/platform · error

Could not find a latest revision for training ${object.code}

Error message

Could not find a latest revision for training ${object.code}

What it means

Thrown by the training draft action when a query for the Training document's latest revision (sorted by revision descending) returns nothing. The library requires an existing latest revision to base the new draft/revision creation on; without it the operation cannot proceed safely. It is a data/model invariant violation, not a client error.

Source

Thrown at plugins/training-resources/src/actions/trainingDraftAction.ts:43

  async (object: Training) => {
    const client = getClient()
    const currentEmployeeRef = getCurrentEmployeeRef()

    // TODO: Move to server plugins when we have them to avoid concurrency
    const latestRevision = await client.findOne(
      object._class,
      {
        space: object.space,
        code: object.code
      },
      {
        sort: {
          revision: SortingOrder.Descending
        }
      }
    )
    if (latestRevision === undefined) {
      throw new Error(`Could not find a latest revision for training ${object.code}`)
    }

    const ops = client.apply()
    const newTrainingRef = await ops.createDoc(object._class, object.space, {
      title: object.title,
      description: object.description,
      attachments: 0,
      passingScore: object.passingScore,
      code: object.code,
      state: TrainingState.Draft,
      releasedBy: null,
      releasedOn: null,
      questions: 0,
      requests: 0,
      revision: latestRevision.revision + 1,
      owner: currentEmployeeRef,
      author: currentEmployeeRef
    })

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Ensure an initial revision exists for the training before invoking the draft action (create the base revision document).
  2. Verify the training's code matches an actual Training document with revisions in the target space.
  3. Check the lookup query filters (attachedTo class, space, sorting on revision) against your data model.
  4. Restore missing revision data from backup or re-run the seeding/migration that generates revisions.

Example fix

// before
const ops = client.apply() // assumes latestRevision exists
// after
if (latestRevision === undefined) {
  await client.createDoc(training.class.Revision, object.space, { attachedTo: object._id, revision: 1 })
}
const ops = client.apply()
Defensive patterns

Strategy: validation

Validate before calling

const latest = await client.findOne(training.class.Revision, { attachedTo: object._id }, { sort: { revision: SortingOrder.Descending } })
if (latest === undefined) throw new Error(`Training ${object.code} has no revisions; seed an initial revision first`)

Type guard

function hasLatestRevision(r: Revision | undefined): r is Revision { return r !== undefined }

Try / catch

try { await runDraftAction(object) } catch (e) { if (e.message.includes('latest revision')) await seedInitialRevision(object); else throw e }

Prevention

When it happens

Trigger: Calling the draft creation action for a training whose code exists but which has no revision documents attached (no Sequences/revisions created yet, revisions deleted, or wrong class/space queried).

Common situations: Running against a test/dev database where revisions were never seeded; a migration that dropped revision objects; querying with an incorrect attachedTo class or space filter; the training was created without an initial revision step.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/cd96829a1a62abc0. Report an issue: GitHub.