plandex-ai/plandex · error

Error getting number of org owners: {err.Error()}

Error message

Error getting number of org owners: {err.Error()}

What it means

When the target user is an org owner, the handler counts owners via db.NumUsersWithRole(auth.OrgId, orgOwnerRoleId); that count query returned an error, so the last-owner safety check cannot complete and the request fails with HTTP 500.

Source

Thrown at app/server/handlers/users.go:177

		http.Error(w, "User "+userId+" is not a member of org "+auth.OrgId, http.StatusForbidden)
		return
	}

	orgOwnerRoleId, err := db.GetOrgOwnerRoleId()

	if err != nil {
		log.Printf("Error getting org owner role id: %v\n", err)
		http.Error(w, "Error getting org owner role id: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// verify user isn't the only org owner
	if orgUser.OrgRoleId == orgOwnerRoleId {
		numOwners, err := db.NumUsersWithRole(auth.OrgId, orgOwnerRoleId)

		if err != nil {
			log.Printf("Error getting number of org owners: %v\n", err)
			http.Error(w, "Error getting number of org owners: "+err.Error(), http.StatusInternalServerError)
			return
		}

		if numOwners == 1 {
			log.Println("Cannot delete the only org owner")
			http.Error(w, "Cannot delete the only org owner", http.StatusForbidden)
			return
		}
	}

	err = db.WithTx(r.Context(), "delete org user", func(tx *sqlx.Tx) error {

		err = db.DeleteOrgUser(auth.OrgId, userId, tx)

		if err != nil {
			log.Println("Error deleting org user: ", err)
			return fmt.Errorf("error deleting org user: %v", err)
		}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check server logs for the wrapped NumUsersWithRole error (timeout vs connection vs syntax)
  2. Verify DB health and connection pool settings; retry after transient failures
  3. Confirm the org_users schema columns used by the count query still match migrations
  4. If recurring under load, index org_users(org_id, org_role_id) to speed the count
Defensive patterns

Strategy: retry

Validate before calling

var n int
err := db.Get(&n,
  "SELECT COUNT(*) FROM org_users WHERE org_id=$1 AND org_role_id=$2",
  orgId, ownerRoleId)
if err == nil {
    fmt.Printf("org has %d owners\n", n)
}

Try / catch

numOwners, err := db.NumUsersWithRole(auth.OrgId, orgOwnerRoleId)
if err != nil {
    if isTransientDBError(err) {
        // retry with backoff before failing the request
    }
    http.Error(w, "internal error", http.StatusInternalServerError)
    return
}

Prevention

When it happens

Trigger: NumUsersWithRole fails during a DELETE of an owner-role user — DB connection failure, timeout, or schema problem in the count query.

Common situations: DB under heavy load causing count-query timeouts; connection pool exhausted by concurrent requests; migration altered the column the count query filters on.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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