{"record":{"id":"dacaca72529b768f","repo":"plandex-ai/plandex","slug":"error-creating-org-v-dacaca","errorCode":null,"errorMessage":"error creating org: %v","messagePattern":"error creating org: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/org_helpers.go","lineNumber":113,"sourceCode":"\nfunc CreateOrg(req *shared.CreateOrgRequest, userId string, domain *string, tx *sqlx.Tx) (*Org, error) {\n\torg := &Org{\n\t\tName:               req.Name,\n\t\tDomain:             domain,\n\t\tAutoAddDomainUsers: req.AutoAddDomainUsers,\n\t\tOwnerId:            userId,\n\t}\n\n\terr := tx.QueryRow(\"INSERT INTO orgs (name, domain, auto_add_domain_users, owner_id, is_trial) VALUES ($1, $2, $3, $4, false) RETURNING id\", req.Name, domain, req.AutoAddDomainUsers, userId).Scan(&org.Id)\n\n\tif err != nil {\n\t\tif IsNonUniqueErr(err) {\n\t\t\t// Handle the uniqueness constraint violation\n\t\t\treturn nil, fmt.Errorf(\"an org with domain %s already exists\", *domain)\n\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"error creating org: %v\", err)\n\t}\n\n\torgOwnerRoleId, err := GetOrgOwnerRoleId()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting org owner role id: %v\", err)\n\t}\n\n\t_, err = tx.Exec(\"INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES ($1, $2, $3)\", org.Id, userId, orgOwnerRoleId)\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error adding org membership: %v\", err)\n\t}\n\n\treturn org, nil\n}\n\nfunc GetOrgForDomain(domain string) (*Org, error) {\n\tvar org Org","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/org_helpers.go#L95-L131","documentation":"CreateOrg wraps a non-unique-constraint failure from the INSERT INTO orgs query. The row insert itself failed (DB connectivity, constraint other than uniqueness, bad data) so the org was not created; unique-name conflicts are handled separately.","triggerScenarios":"INSERT INTO orgs fails for reasons other than uniqueness: connection lost mid-transaction, not-null/enum constraint on name violated (empty req.Name), permission denied for the DB role, or tx already aborted by an earlier statement.","commonSituations":"Empty or over-length org name violating a CHECK/length constraint; migration drift leaving columns missing; transaction aborted earlier in the request so every subsequent statement fails; DB user lacking INSERT privilege on orgs.","solutions":["Read the wrapped %v message and map it to the exact Postgres error code (e.g. 23502 not-null, 42501 permission)","Validate req.Name is non-empty and within length limits before calling CreateOrg","Ensure migrations are up to date so the orgs schema matches the code","Check the transaction's prior statements — if tx was already aborted, fix the earlier error rather than this one"],"exampleFix":"// before\nreq := &shared.CreateOrgRequest{Name: \"\"}\norg, err := db.CreateOrg(req, userId, &domain, tx)\n// after\nif req.Name == \"\" || len(req.Name) > 100 {\n    return errors.New(\"org name is required (max 100 chars)\")\n}\norg, err := db.CreateOrg(req, userId, &domain, tx)","handlingStrategy":"validation","validationCode":"if req.Name == \"\" || len(req.Name) > 100 { return errors.New(\"invalid org name\") }\nif tx == nil { return errors.New(\"CreateOrg requires a transaction\") }","typeGuard":"func isCreateOrgErr(err error) bool { return strings.HasPrefix(err.Error(), \"error creating org: \") }","tryCatchPattern":"org, err := db.CreateOrg(req, userId, &domain, tx)\nif err != nil {\n    log.Printf(\"create org failed: %v\", err) // includes wrapped pq error\n    return fmt.Errorf(\"could not create organization\")\n}","preventionTips":["Validate request fields (name length, domain format) server-side before DB writes","Keep schema migrations in sync with the Org struct","Check for earlier tx failures that abort subsequent statements","Grant the app DB role INSERT on orgs"],"tags":["database","postgresql","insert-failure"],"backgroundTag":"database-query-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"}