{"record":{"id":"403abd270cdf08f3","repo":"plandex-ai/plandex","slug":"error-getting-orgs-for-user-v","errorCode":null,"errorMessage":"error getting orgs for user: %v","messagePattern":"error getting orgs for user: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/org_helpers.go","lineNumber":24,"sourceCode":"\t\"log\"\n\t\"strings\"\n\n\tshared \"plandex-shared\"\n\n\t\"github.com/jmoiron/sqlx\"\n\t\"github.com/lib/pq\"\n)\n\nconst orgFields = \"id, name, domain, auto_add_domain_users, owner_id, is_trial, created_at, updated_at\"\n\nfunc GetAccessibleOrgsForUser(user *User) ([]*Org, error) {\n\t// direct access\n\tvar orgUsers []*OrgUser\n\tvar orgs []*Org\n\n\terr := Conn.Select(&orgUsers, \"SELECT * FROM orgs_users WHERE user_id = $1\", user.Id)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting orgs for user: %v\", err)\n\t}\n\n\torgRoleIdByOrgId := map[string]string{}\n\torgIds := []string{}\n\tfor _, ou := range orgUsers {\n\t\torgIds = append(orgIds, ou.OrgId)\n\t\torgRoleIdByOrgId[ou.OrgId] = ou.OrgRoleId\n\t}\n\n\tif len(orgIds) > 0 {\n\t\tquery := fmt.Sprintf(\"SELECT %s FROM orgs WHERE id = ANY($1)\", orgFields)\n\t\terr = Conn.Select(&orgs, query, pq.Array(orgIds))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error getting orgs for user: %v\", err)\n\t\t}\n\t} else {\n\t\tlog.Println(\"No orgs found for user\")\n\t\treturn orgs, nil","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/org_helpers.go#L6-L42","documentation":"GetAccessibleOrgsForUser wraps a failure of the initial SELECT on orgs_users (direct memberships) for the user. Any query error — connection, missing table, or scan mismatch on OrgUser — is wrapped with this message. An empty result is NOT an error; that path continues to the invite lookup.","triggerScenarios":"SELECT * FROM orgs_users fails because the table doesn't exist, the DB is unreachable, or OrgUser struct fields no longer match orgs_users columns (scan error).","commonSituations":"Migrations skipped in a new environment; orgs_users schema changed (column added/renamed) while OrgUser struct not updated; DB outage during login/org listing.","solutions":["Check the wrapped error text: 'relation orgs_users does not exist' → run migrations; 'missing destination name' → fix OrgUser db tags","Run pending migrations","Align OrgUser struct fields/tags with the orgs_users schema","Verify DB connectivity and retry transient failures"],"exampleFix":"// before\nerr := Conn.Select(&orgUsers, \"SELECT * FROM orgs_users WHERE user_id = $1\", user.Id)\n// after\nerr := Conn.Select(&orgUsers, \"SELECT org_id, user_id, org_role_id FROM orgs_users WHERE user_id = $1\", user.Id) // explicit columns matching OrgUser db tags","handlingStrategy":"try-catch","validationCode":"// startup preflight\nvar n int\nif err := Conn.Get(&n, \"SELECT count(*) FROM information_schema.tables WHERE table_name = 'orgs_users'\"); err != nil || n == 0 {\n    log.Fatal(\"orgs_users table missing — run migrations\")\n}","typeGuard":"func isScanError(err error) bool {\n    return err != nil && (strings.Contains(err.Error(), \"unsupported Scan\") || strings.Contains(err.Error(), \"missing destination name\"))\n}","tryCatchPattern":"orgs, err := db.GetAccessibleOrgsForUser(user)\nif err != nil {\n    if strings.Contains(err.Error(), \"error getting orgs for user\") && isScanError(errors.Unwrap(err)) {\n        log.Printf(\"OrgUser struct/schema mismatch: %v\", err)\n    }\n    http.Error(w, \"could not load orgs\", http.StatusInternalServerError)\n    return\n}","preventionTips":["Keep the OrgUser struct db tags in sync with orgs_users columns","Apply migrations in every environment before serving traffic","Use explicit column lists instead of SELECT *","Add DB health/readiness checks before accepting logins","Wrap with %w so callers can errors.Is/As the root cause"],"tags":["database","postgres","sqlx","scan-error"],"backgroundTag":"sql-scan-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"}