{"record":{"id":"ff0bfd89256b3677","repo":"mastra-ai/mastra","slug":"schedule-schedule-id-already-exists","errorCode":null,"errorMessage":"Schedule ${schedule.id} already exists","messagePattern":"Schedule (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/schedules/inmemory.ts","lineNumber":32,"sourceCode":"  return copy;\n}\n\nexport class InMemorySchedulesStorage extends SchedulesStorage {\n  private db: InMemoryDB;\n\n  constructor({ db }: { db: InMemoryDB }) {\n    super();\n    this.db = db;\n  }\n\n  async dangerouslyClearAll(): Promise<void> {\n    this.db.schedules.clear();\n    this.db.scheduleTriggers.length = 0;\n  }\n\n  async createSchedule(schedule: Schedule): Promise<Schedule> {\n    if (this.db.schedules.has(schedule.id)) {\n      throw new Error(`Schedule ${schedule.id} already exists`);\n    }\n    const stored = clone(schedule);\n    this.db.schedules.set(stored.id, stored);\n    return clone(stored);\n  }\n\n  async getSchedule(id: string): Promise<Schedule | null> {\n    const found = this.db.schedules.get(id);\n    return found ? cloneRow(found) : null;\n  }\n\n  async listSchedules(filter?: ScheduleFilter): Promise<Schedule[]> {\n    let rows = Array.from(this.db.schedules.values());\n    if (filter?.status) {\n      rows = rows.filter(r => r.status === filter.status);\n    }\n    if (filter?.workflowId) {\n      rows = rows.filter(r => r.target.type === 'workflow' && r.target.workflowId === filter.workflowId);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/schedules/inmemory.ts#L14-L50","documentation":"InMemoryScheduleStorage.createSchedule() throws when a schedule with the same id already exists in the schedules map. Schedule ids are primary keys; the store refuses silent overwrites so callers don't accidentally replace an existing schedule definition.","triggerScenarios":"Calling createSchedule({ id: 'sched-1', ... }) twice with the same id; re-running a registration/startup routine that creates schedules on every boot; a makeDue/makeDue test helper creating a schedule whose id collides with a pre-seeded one.","commonSituations":"App restart re-registering cron-like schedules into a still-populated in-memory store; duplicated constants for schedule ids; parallel test suites sharing one storage instance.","solutions":["Generate unique ids per schedule (crypto.randomUUID()) instead of fixed strings.","Check existence first (listSchedules / getSchedule) and skip or update instead of create.","On restart, upsert: catch this error and call updateSchedule(id, patch) as a fallback.","Call clearSchedules() (or use a fresh storage instance) between test runs."],"exampleFix":"// before\nawait storage.schedules.createSchedule({ id: 'nightly-job', ... });\n// after\nconst id = crypto.randomUUID();\nawait storage.schedules.createSchedule({ id, ...scheduleDef });","handlingStrategy":"try-catch","validationCode":"const existing = await storage.schedules.listSchedules();\nif (existing.schedules?.some(s => s.id === schedule.id)) {\n  throw new Error(`schedule ${schedule.id} already registered`);\n}","typeGuard":"function scheduleExists(schedules: { id: string }[], id: string): boolean {\n  return schedules.some(s => s.id === id);\n}","tryCatchPattern":"try {\n  await storage.schedules.createSchedule(schedule);\n} catch (e) {\n  if (e instanceof Error && /^Schedule .+ already exists$/.test(e.message)) {\n    await storage.schedules.updateSchedule(schedule.id, { ...schedule });\n    return;\n  }\n  throw e;\n}","preventionTips":["Use crypto.randomUUID() for schedule ids instead of fixed strings.","Make startup registration idempotent: check-then-create or catch-and-update.","Clear in-memory schedules between test runs (clearSchedules / fresh instance).","Keep schedule id constants in one place to avoid accidental duplicates."],"tags":["storage","duplicate-key","schedules","in-memory"],"backgroundTag":"duplicate-resource-id","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}