{"record":{"id":"13fc18046c3e109a","repo":"Budibase/budibase","slug":"unable-to-retrieve-prod-db-no-workspace-id","errorCode":null,"errorMessage":"Unable to retrieve prod DB - no workspace ID.","messagePattern":"Unable to retrieve prod DB - no workspace ID\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/backend-core/src/context/mainContext.ts","lineNumber":433,"sourceCode":"  if (!workspaceId) {\n    throw new Error(\"Unable to retrieve workspace DB - no workspace ID.\")\n  }\n  if (isSelfHostUsingCloud()) {\n    throw new Error(\n      \"Workspace DB not found - self-host users using cloud don't have workspace DBs\"\n    )\n  }\n  return getDB(workspaceId, opts)\n}\n\n/**\n * This specifically gets the prod workspace ID, if the request\n * contained a development workspace ID, this will get the prod one.\n */\nexport function getProdWorkspaceDB(opts?: any): Database {\n  const workspaceId = getWorkspaceId()\n  if (!workspaceId) {\n    throw new Error(\"Unable to retrieve prod DB - no workspace ID.\")\n  }\n  return getDB(conversions.getProdWorkspaceID(workspaceId), opts)\n}\n\n/**\n * This specifically gets the dev workspace ID, if the request\n * contained a prod workspace ID, this will get the dev one.\n */\nexport function getDevWorkspaceDB(opts?: any): Database {\n  const workspaceId = getWorkspaceId()\n  if (!workspaceId) {\n    throw new Error(\"Unable to retrieve dev DB - no workspace ID.\")\n  }\n  return getDB(conversions.getDevWorkspaceID(workspaceId), opts)\n}\n\nexport function isScim(): boolean {\n  const context = Context.get()","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/backend-core/src/context/mainContext.ts#L415-L451","documentation":"getProdWorkspaceDB converts the current context workspace ID to its production form and opens that DB. It throws when the ambient request context contains no workspace ID at all, because there is nothing to convert or open.","triggerScenarios":"Calling getProdWorkspaceDB (aliases prodDb/database/dbs/productionDb) outside a workspace-scoped request — e.g. in global/tenant-only endpoints, background jobs, or tests where setWorkspaceId/context was never established.","commonSituations":"Middleware not populating workspace context (missing app ID header/path), calling prod DB helpers from tenant-level code (users, auth, global config), background tasks that lose the request context.","solutions":["Ensure the code path runs inside a request with a workspace ID (app id header/url param) and that context middleware ran","If the code is genuinely workspace-agnostic, use getDB with an explicit DB name instead of context helpers","In scripts/workers, explicitly establish context with the target workspace ID before calling"],"exampleFix":"// before\nconst db = getProdWorkspaceDB()\n// after\nconst workspaceId = getWorkspaceId()\nif (!workspaceId) {\n  throw new Error(\"No workspace in context; pass an explicit DB name\")\n}\nconst db = getProdWorkspaceDB()","handlingStrategy":"validation","validationCode":"import { getWorkspaceId } from \"@budibase/backend-core/context\"\nif (!getWorkspaceId()) {\n  throw new Error(\"prod DB requested outside workspace context\")\n}\nconst db = getProdWorkspaceDB()","typeGuard":"const hasWorkspaceContext = (): boolean => !!getWorkspaceId()","tryCatchPattern":"try {\n  const db = getProdWorkspaceDB()\n} catch (err) {\n  if (err.message.includes(\"no workspace ID\")) {\n    // fall back to explicit getDB(name) or return 400 to caller\n  } else throw err\n}","preventionTips":["Only call context DB helpers inside workspace-scoped routes/handlers","For background jobs, propagate and restore workspace context explicitly","Add a test asserting context middleware runs before DB access"],"tags":["request-context","database-context","missing-id"],"backgroundTag":"missing-workspace-context","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}