{"record":{"id":"372e825ce75f67f7","repo":"hcengineering/platform","slug":"sequence-for-training-class-training-not-found","errorCode":null,"errorMessage":"Sequence for ${training.class.Training} not found","messagePattern":"Sequence for (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/training-resources/src/utils/getNextTrainingSeqNumber.ts","lineNumber":14,"sourceCode":"//\n// Copyright @ 2024 Hardcore Engineering Inc.\n//\n\nimport { getClient } from '@hcengineering/presentation'\nimport core, { type Sequence } from '@hcengineering/core'\nimport training from '../plugin'\n\nexport async function getNextTrainingSeqNumber (): Promise<number> {\n  const client = getClient()\n\n  const sequence = await client.findOne(core.class.Sequence, { attachedTo: training.class.Training })\n  if (sequence === undefined) {\n    throw new Error(`Sequence for ${training.class.Training} not found`)\n  }\n\n  const inc = await client.update(sequence, { $inc: { sequence: 1 } }, true)\n\n  return (inc as { object: Sequence }).object.sequence\n}\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/training-resources/src/utils/getNextTrainingSeqNumber.ts#L1-L21","documentation":"getNextTrainingSeqNumber looks up the core Sequence document attached to training.class.Training and throws when none exists. Sequences allocate monotonic training numbers by incrementing a counter; without the Sequence document numbering cannot proceed. The Sequence must be created by the model setup/migration.","triggerScenarios":"Calling createTraining (which calls getNextTrainingSeqNumber) on a workspace where the training Sequence document was never seeded or was deleted.","commonSituations":"Fresh/test databases without full model seeding; migrations that reset sequences; someone deleting the Sequence object manually; upgrading a project where sequence creation step was added later.","solutions":["Create the missing Sequence document attached to training.class.Training (or re-run the model migration that seeds it).","Verify with a query: findOne(core.class.Sequence, { attachedTo: training.class.Training }).","Check that domain migration/seeding scripts ran on this workspace.","Wrap training creation to surface a clear setup error when sequences are absent."],"exampleFix":"// before\nconst seqNumber = await getNextTrainingSeqNumber()\n// after\nlet seq = await client.findOne(core.class.Sequence, { attachedTo: training.class.Training })\nif (seq === undefined) {\n  seq = await client.createDoc(core.class.Sequence, core.space.Model, { attachedTo: training.class.Training, sequence: 0 })\n}\nconst seqNumber = seq.sequence + 1","handlingStrategy":"validation","validationCode":"const seq = await client.findOne(core.class.Sequence, { attachedTo: training.class.Training })\nif (seq === undefined) throw new Error('Training sequence not seeded; run model migration')","typeGuard":"function sequenceExists(s: Sequence | undefined): s is Sequence { return s !== undefined }","tryCatchPattern":"try { return await createTraining(data) } catch (e) { if (e.message.includes('Sequence')) await seedSequences(); else throw e }","preventionTips":["Ensure sequence-seeding migrations run on every workspace","Never delete core Sequence documents","Add a health check that all required sequences exist"],"tags":["missing-object","sequence","training","data-migration"],"backgroundTag":"missing-sequence-record","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}