{"record":{"id":"49df9b8fb880926b","repo":"mastra-ai/mastra","slug":"workspace-with-id-workspace-id-already-exists","errorCode":null,"errorMessage":"Workspace with id ${workspace.id} already exists","messagePattern":"Workspace with id (.+?) already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/workspaces/inmemory.ts","lineNumber":49,"sourceCode":"  async dangerouslyClearAll(): Promise<void> {\n    this.db.workspaces.clear();\n    this.db.workspaceVersions.clear();\n  }\n\n  // ==========================================================================\n  // Workspace CRUD Methods\n  // ==========================================================================\n\n  async getById(id: string): Promise<StorageWorkspaceType | null> {\n    const config = this.db.workspaces.get(id);\n    return config ? this.deepCopyConfig(config) : null;\n  }\n\n  async create(input: { workspace: StorageCreateWorkspaceInput }): Promise<StorageWorkspaceType> {\n    const { workspace } = input;\n\n    if (this.db.workspaces.has(workspace.id)) {\n      throw new Error(`Workspace with id ${workspace.id} already exists`);\n    }\n\n    const now = new Date();\n    const newConfig: StorageWorkspaceType = {\n      id: workspace.id,\n      status: 'draft',\n      activeVersionId: undefined,\n      authorId: workspace.authorId,\n      metadata: workspace.metadata,\n      createdAt: now,\n      updatedAt: now,\n    };\n\n    this.db.workspaces.set(workspace.id, newConfig);\n\n    // Extract config fields from the flat input (everything except record fields)\n    const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = workspace;\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/workspaces/inmemory.ts#L31-L67","documentation":"`create` in the workspaces storage enforces unique workspace IDs: if `this.db.workspaces` already holds `workspace.id`, it throws instead of overwriting. Workspace IDs are primary keys, so duplicates indicate an id-generation or retry bug.","triggerScenarios":"Calling `create({ workspace: { id: '<existing-id>', ... } })` where a workspace with that ID already exists, including via `seedAgent` flows that regenerate fixed IDs.","commonSituations":"Re-running seed/bootstrap scripts with hard-coded workspace IDs (e.g. 'default'), retrying a create request that already succeeded, or app initialization code running twice in the same process.","solutions":["Check existence first (`getWorkspace(id)`) and skip or return the existing workspace.","Generate IDs with `crypto.randomUUID()` rather than fixed names in seed scripts.","Catch the error and treat as idempotent when the existing workspace matches what you'd create.","Guard app initialization so bootstrap/seeding runs only once per process."],"exampleFix":"// before\nawait storage.create({ workspace: { id: 'default', name: 'Default' } }); // throws on re-run\n// after\nconst existing = await storage.getWorkspace('default');\nif (!existing) await storage.create({ workspace: { id: 'default', name: 'Default' } });","handlingStrategy":"fallback","validationCode":"async function getOrCreateWorkspace(storage, workspace) {\n  const existing = await storage.getWorkspace(workspace.id);\n  if (existing) return existing;\n  return storage.create({ workspace });\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await storage.create({ workspace });\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Workspace with id')) {\n    return storage.getWorkspace(workspace.id); // already exists: idempotent\n  }\n  throw err;\n}","preventionTips":["Make bootstrap/seed code idempotent: check existence before create.","Use UUIDs for workspace IDs in tests and seeds, or run seeding only once.","Guard initialization with a once-flag so app startup can't double-create."],"tags":["duplicate-key","idempotency","storage"],"backgroundTag":"duplicate-record-id","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}