{"record":{"id":"d1fd9a8fefae09bb","repo":"plandex-ai/plandex","slug":"error-adding-org-domain-users-v","errorCode":null,"errorMessage":"error adding org domain users: %v","messagePattern":"error adding org domain users: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/orgs.go","lineNumber":115,"sourceCode":"\t\t\t}\n\n\t\t\tdomain = &auth.User.Domain\n\t\t}\n\n\t\t// create a new org\n\t\torg, err = db.CreateOrg(&req, auth.AuthToken.UserId, domain, tx)\n\n\t\tif err != nil {\n\t\t\tlog.Printf(\"Error creating org: %v\\n\", err)\n\t\t\treturn fmt.Errorf(\"error creating org: %v\", err)\n\t\t}\n\n\t\tif org.AutoAddDomainUsers && org.Domain != nil {\n\t\t\terr = db.AddOrgDomainUsers(org.Id, *org.Domain, tx)\n\n\t\t\tif err != nil {\n\t\t\t\tlog.Printf(\"Error adding org domain users: %v\\n\", err)\n\t\t\t\treturn fmt.Errorf(\"error adding org domain users: %v\", err)\n\t\t\t}\n\t\t}\n\n\t\t_, apiErr = hooks.ExecHook(hooks.CreateOrg, hooks.HookParams{\n\t\t\tAuth: auth,\n\t\t\tTx:   tx,\n\n\t\t\tCreateOrgHookRequestParams: &hooks.CreateOrgHookRequestParams{\n\t\t\t\tOrg: org,\n\t\t\t},\n\t\t})\n\n\t\treturn nil\n\t})\n\n\tif apiErr != nil {\n\t\twriteApiError(w, *apiErr)\n\t\treturn","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/orgs.go#L97-L133","documentation":"Wrapped error from the create-org transaction when db.AddOrgDomainUsers fails while adding existing users with the org's domain to the new org. The transaction rolls back (including org creation) and the handler returns 500.","triggerScenarios":"Creating an org with autoAddDomainUsers=true (and a valid non-email-service domain), where the bulk insert of domain users into org membership fails — FK mismatch, connection loss, or a very large user set timing out.","commonSituations":"Domains with thousands of users causing long-running bulk inserts; DB connectivity issues; schema drift on the org membership table.","solutions":["Inspect the wrapped error in logs to pinpoint the DB failure","Retry org creation; the transaction rollback guarantees no partial org","If the domain has many users, consider batching AddOrgDomainUsers or adding them asynchronously post-creation","Check DB connectivity/pool and membership-table schema/constraints"],"exampleFix":"// before: one bulk insert inside the tx can time out\nerr = db.AddOrgDomainUsers(org.Id, *org.Domain, tx)\n// after: add users asynchronously after commit\ncreateOrg(); go addDomainUsersInBackground(org.Id, *org.Domain)","handlingStrategy":"retry","validationCode":"const domain = userEmail.split('@')[1]\nif (!domain || /@(gmail|outlook|hotmail|yahoo)\\./i.test(userEmail)) {\n  req.autoAddDomainUsers = false\n}","typeGuard":"function hasCustomDomain(userEmail) {\n  return /^[^@]+@([a-z0-9-]+\\.)+[a-z]{2,}$/i.test(userEmail) &&\n    !/@(gmail|googlemail|outlook|hotmail|yahoo)\\./i.test(userEmail)\n}","tryCatchPattern":"try {\n  org = await client.CreateOrg(req)\n} catch (err) {\n  if (String(err.message).includes('error adding org domain users')) {\n    await new Promise(r => setTimeout(r, 1000))\n    org = await client.CreateOrg(req) // tx rolled back fully; safe to retry\n  }\n}","preventionTips":["Create the org with autoAddDomainUsers=false and add domain users asynchronously for large domains","Retry idempotently — the transaction rollback means no partial org exists after failure","Watch DB connection-pool health during bulk membership operations"],"tags":["database","transaction","orgs"],"backgroundTag":"db-transaction-aborted","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}