{"record":{"id":"caa6d4525ddc0b37","repo":"plandex-ai/plandex","slug":"error-creating-org-v-caa6d4","errorCode":null,"errorMessage":"error creating org: %v","messagePattern":"error creating org: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/orgs.go","lineNumber":107,"sourceCode":"\tvar org *db.Org\n\terr = db.WithTx(r.Context(), \"create org\", func(tx *sqlx.Tx) error {\n\t\tvar err error\n\t\tvar domain *string\n\t\tif req.AutoAddDomainUsers {\n\t\t\tif shared.IsEmailServiceDomain(auth.User.Domain) {\n\t\t\t\tlog.Printf(\"Invalid domain: %v\\n\", auth.User.Domain)\n\t\t\t\treturn fmt.Errorf(\"invalid domain: %v\", auth.User.Domain)\n\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},","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/orgs.go#L89-L125","documentation":"Wrapped error from the create-org transaction when db.CreateOrg fails to insert the new org row. The WithTx closure returns this error, the transaction rolls back, and the handler responds 500 'Error creating org'.","triggerScenarios":"POST to create-org where db.CreateOrg(&req, userId, domain, tx) hits a DB error: unique constraint on org name/domain, invalid or conflicting domain value, connection failure, or tx already aborted by an earlier step in the closure.","commonSituations":"Creating an org with a domain another org already claims; DB pool exhaustion; migration drift so the insert references a missing column.","solutions":["Check server logs for the wrapped %v error to see the exact Postgres failure (constraint, connection)","Retry with a different org name/domain if a unique constraint fired","Verify DB connectivity and schema migrations if the error is connection- or column-related","Consider returning 409 for duplicate org/domain instead of 500"],"exampleFix":"// before: duplicate domain\nclient.CreateOrg(shared.CreateOrgRequest{Name: \"acme\", Domain: \"acme.com\"})\n// after: unique name/domain or omit domain\nclient.CreateOrg(shared.CreateOrgRequest{Name: \"acme-2\"})","handlingStrategy":"try-catch","validationCode":"// pre-flight: pick a unique name; send a domain only if you own it\nif (!req.name || req.name.trim() === '') throw new Error('org name required')","typeGuard":null,"tryCatchPattern":"try {\n  org = await client.CreateOrg(req)\n} catch (err) {\n  if (String(err.message).includes('error creating org')) {\n    // unique-constraint or DB failure: change name/domain and retry once\n    req.name = req.name + '-' + Date.now()\n    org = await client.CreateOrg(req)\n  }\n}","preventionTips":["Use a unique org name and only send Domain if you control it","Confirm the server can reach Postgres before bulk org-creation scripts","Handle non-2xx create-org responses instead of assuming success"],"tags":["database","transaction","orgs"],"backgroundTag":"database-insert-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}