{"record":{"id":"b09aed14d62de667","repo":"mastra-ai/mastra","slug":"version-with-id-input-id-already-exists-b09aed","errorCode":null,"errorMessage":"Version with id ${input.id} already exists","messagePattern":"Version with id (.+?) already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/workspaces/inmemory.ts","lineNumber":252,"sourceCode":"    const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n\n    return {\n      workspaces: clonedConfigs.slice(offset, offset + perPage),\n      total: clonedConfigs.length,\n      page,\n      perPage: perPageForResponse,\n      hasMore: offset + perPage < clonedConfigs.length,\n    };\n  }\n\n  // ==========================================================================\n  // Workspace Version Methods\n  // ==========================================================================\n\n  async createVersion(input: CreateWorkspaceVersionInput): Promise<WorkspaceVersion> {\n    // Check if version with this ID already exists\n    if (this.db.workspaceVersions.has(input.id)) {\n      throw new Error(`Version with id ${input.id} already exists`);\n    }\n\n    // Check for duplicate (workspaceId, versionNumber) pair\n    for (const version of this.db.workspaceVersions.values()) {\n      if (version.workspaceId === input.workspaceId && version.versionNumber === input.versionNumber) {\n        throw new Error(`Version number ${input.versionNumber} already exists for workspace ${input.workspaceId}`);\n      }\n    }\n\n    const version: WorkspaceVersion = {\n      ...input,\n      createdAt: new Date(),\n    };\n\n    // Deep clone before storing\n    this.db.workspaceVersions.set(input.id, this.deepCopyVersion(version));\n    return this.deepCopyVersion(version);\n  }","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/workspaces/inmemory.ts#L234-L270","documentation":"createVersion() rejects a new version whose id already exists in the version table. Version ids are primary keys; inserting a duplicate would overwrite or corrupt version history, so the domain throws eagerly.","triggerScenarios":"Calling createVersion({ id: '<existing-version-id>', ... }) with a reused or hardcoded id; retrying a createVersion call that actually succeeded the first time; generating ids with Math.random()-style code that collides in tests; passing the workspace id as the version id.","commonSituations":"Custom migration scripts replaying version dumps without deduplicating; test fixtures re-seeding the same fixture id into a shared storage instance; idempotency attempts that reuse ids instead of checking existence first.","solutions":["Generate a fresh unique id per version (crypto.randomUUID()) instead of reusing one","Check existence first: if the version id already exists, skip or fetch the existing version instead of inserting","Make createVersion idempotent in your code path by catching this error and treating it as 'already applied' for retried migrations","Deduplicate version dumps before replaying them into storage"],"exampleFix":"// before\nawait storage.workspaces.createVersion({ id: versionId, workspaceId, versionNumber, config });\n// after\nconst existing = await storage.workspaces.getVersionById(versionId);\nif (!existing) {\n  await storage.workspaces.createVersion({ id: crypto.randomUUID(), workspaceId, versionNumber, config });\n}","handlingStrategy":"try-catch","validationCode":"const exists = await storage.workspaces.getVersionById(versionId);\nif (exists) return exists; // idempotent no-op","typeGuard":null,"tryCatchPattern":"try {\n  await storage.workspaces.createVersion({ id: newId, workspaceId, versionNumber, config });\n} catch (err) {\n  if (err instanceof Error && err.message === `Version with id ${newId} already exists`) {\n    return; // already applied — treat as idempotent success\n  }\n  throw err;\n}","preventionTips":["Always generate version ids with crypto.randomUUID()","Never reuse ids from exports/dumps without deduplication","Capture ids returned by create() instead of inventing them","Make migration scripts idempotent by checking existence before insert"],"tags":["storage","workspace","duplicate-key","versioning"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}