{"record":{"id":"ef741da3426ec7c5","repo":"Significant-Gravitas/AutoGPT","slug":"workspace-ws-id-does-not-belong-to-org-org-id","errorCode":null,"errorMessage":"Workspace {ws_id} does not belong to org {org_id}","messagePattern":"Workspace (.+?) does not belong to org (.+?)","errorType":"http","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"autogpt_platform/backend/backend/api/features/orgs/team_db.py","lineNumber":163,"sourceCode":"        where={\"teamId\": ws_id, \"status\": \"ACTIVE\"},\n        include={\"User\": True},\n    )\n    return [TeamMemberResponse.from_db(m) for m in members]\n\n\nasync def add_team_member(\n    ws_id: str,\n    user_id: str,\n    org_id: str,\n    is_admin: bool = False,\n    is_billing_manager: bool = False,\n    invited_by: str | None = None,\n) -> TeamMemberResponse:\n    \"\"\"Add a member to a workspace. Must be an org member, workspace must belong to org.\"\"\"\n    # Verify workspace belongs to the org\n    ws = await prisma.team.find_unique(where={\"id\": ws_id})\n    if ws is None or ws.orgId != org_id:\n        raise ValueError(f\"Workspace {ws_id} does not belong to org {org_id}\")\n\n    # Verify user is in the org\n    org_member = await prisma.orgmember.find_unique(\n        where={\"orgId_userId\": {\"orgId\": org_id, \"userId\": user_id}}\n    )\n    if org_member is None:\n        raise ValueError(f\"User {user_id} is not a member of the organization\")\n\n    member = await prisma.teammember.create(\n        data={\n            \"teamId\": ws_id,\n            \"userId\": user_id,\n            \"isAdmin\": is_admin,\n            \"isBillingManager\": is_billing_manager,\n            \"status\": \"ACTIVE\",\n            \"invitedByUserId\": invited_by,\n        },\n        include={\"User\": True},","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/Significant-Gravitas/AutoGPT/blob/9c8bb5550f446ba5d3046b78896578742495b3cf/autogpt_platform/backend/backend/api/features/orgs/team_db.py#L145-L181","documentation":"Raised by team_db.add_team_member when the Team row for ws_id is missing OR its orgId differs from the passed org_id. One guard covering both conditions: the workspace must exist and belong to the organization you are adding within. Plain ValueError from the DB layer.","triggerScenarios":"POST /api/orgs/{org_id}/workspaces/{ws_id}/members where ws_id belongs to another org or does not exist. Note the condition is ws is None or ws.orgId != org_id — an already-deleted workspace produces the same 'does not belong' wording rather than a NotFoundError.","commonSituations":"Admin UI where the org dropdown and workspace picker hold unrelated selections; concurrent deletion of the workspace between list and submit; fixtures pairing a workspace from org A with org B's id.","solutions":["Re-fetch the workspace through GET /api/orgs/{org_id}/workspaces/{ws_id} before showing the add-member form; failure means the pair is invalid","Build the add-member URL from one TeamResponse (orgId + id) so the pair cannot diverge","On failure, refresh the admin's workspace list — the workspace may have been deleted or moved","In tests, create the workspace with create_team(org_id, ...) so orgId matches by construction"],"exampleFix":"// before\nconst url = `/api/orgs/${orgId}/workspaces/${selectedWsId}/members`;\n// after\nconst ws = await getWorkspace(orgId, selectedWsId); // throws first if mismatch\nconst url = `/api/orgs/${ws.orgId}/workspaces/${ws.id}/members`;","handlingStrategy":"validation","validationCode":"const ws = await getWorkspace(orgId, wsId).catch(() => null);\nif (!ws || ws.orgId !== orgId) throw new Error('Invalid org/workspace pair');","typeGuard":"function workspaceInOrg(ws: TeamResponse, orgId: string): boolean {\n  return ws.orgId === orgId;\n}","tryCatchPattern":"try {\n  await addTeamMember(orgId, wsId, userId);\n} catch (e) {\n  if (e.message.includes('does not belong to org')) {\n    // refresh admin workspace picker; pair is stale\n  } else throw e;\n}","preventionTips":["Derive org/ws ids from one TeamResponse when building add-member URLs","Re-fetch on org switch"],"tags":["workspaces","orgs","members","valueerror"],"backgroundTag":null,"analyzedSha":"9c8bb5550f446ba5d3046b78896578742495b3cf","analyzedAt":"2026-08-14T17:17:21.957Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}