plandex-ai/plandex · error

error getting orgs for user: %v

Error message

error getting orgs for user: %v

What it means

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.

Source

Thrown at app/server/db/org_helpers.go:24

	"log"
	"strings"

	shared "plandex-shared"

	"github.com/jmoiron/sqlx"
	"github.com/lib/pq"
)

const orgFields = "id, name, domain, auto_add_domain_users, owner_id, is_trial, created_at, updated_at"

func GetAccessibleOrgsForUser(user *User) ([]*Org, error) {
	// direct access
	var orgUsers []*OrgUser
	var orgs []*Org

	err := Conn.Select(&orgUsers, "SELECT * FROM orgs_users WHERE user_id = $1", user.Id)
	if err != nil {
		return nil, fmt.Errorf("error getting orgs for user: %v", err)
	}

	orgRoleIdByOrgId := map[string]string{}
	orgIds := []string{}
	for _, ou := range orgUsers {
		orgIds = append(orgIds, ou.OrgId)
		orgRoleIdByOrgId[ou.OrgId] = ou.OrgRoleId
	}

	if len(orgIds) > 0 {
		query := fmt.Sprintf("SELECT %s FROM orgs WHERE id = ANY($1)", orgFields)
		err = Conn.Select(&orgs, query, pq.Array(orgIds))
		if err != nil {
			return nil, fmt.Errorf("error getting orgs for user: %v", err)
		}
	} else {
		log.Println("No orgs found for user")
		return orgs, nil

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check the wrapped error text: 'relation orgs_users does not exist' → run migrations; 'missing destination name' → fix OrgUser db tags
  2. Run pending migrations
  3. Align OrgUser struct fields/tags with the orgs_users schema
  4. Verify DB connectivity and retry transient failures

Example fix

// before
err := Conn.Select(&orgUsers, "SELECT * FROM orgs_users WHERE user_id = $1", user.Id)
// after
err := 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
Defensive patterns

Strategy: try-catch

Validate before calling

// startup preflight
var n int
if err := Conn.Get(&n, "SELECT count(*) FROM information_schema.tables WHERE table_name = 'orgs_users'"); err != nil || n == 0 {
    log.Fatal("orgs_users table missing — run migrations")
}

Type guard

func isScanError(err error) bool {
    return err != nil && (strings.Contains(err.Error(), "unsupported Scan") || strings.Contains(err.Error(), "missing destination name"))
}

Try / catch

orgs, err := db.GetAccessibleOrgsForUser(user)
if err != nil {
    if strings.Contains(err.Error(), "error getting orgs for user") && isScanError(errors.Unwrap(err)) {
        log.Printf("OrgUser struct/schema mismatch: %v", err)
    }
    http.Error(w, "could not load orgs", http.StatusInternalServerError)
    return
}

Prevention

When it happens

Trigger: 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).

Common situations: Migrations skipped in a new environment; orgs_users schema changed (column added/renamed) while OrgUser struct not updated; DB outage during login/org listing.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/403abd270cdf08f3. Report an issue: GitHub.