plandex-ai/plandex · error

error adding org user: %v

Error message

error adding org user: %v

What it means

AddToOrgForDomain wraps an error returned by CreateOrgUser(org.Id, userId, orgOwnerRoleId, tx) with this message. The inner error is the INSERT INTO orgs_users failure (see the 'error adding org member' wrap), re-labeled here with account-creation context.

Source

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

func AddToOrgForDomain(userId, domain string, tx *sqlx.Tx) (string, error) {
	org, err := GetOrgForDomain(domain)

	if err != nil {
		return "", fmt.Errorf("error getting org for domain: %v", err)
	}

	orgOwnerRoleId, err := GetOrgOwnerRoleId()

	if err != nil {
		return "", fmt.Errorf("error getting org owner role id: %v", err)
	}

	if org != nil && org.AutoAddDomainUsers {
		err = CreateOrgUser(org.Id, userId, orgOwnerRoleId, tx)

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

	var orgId string
	if org != nil {
		orgId = org.Id
	}

	return orgId, nil
}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Inspect the nested cause: unique-constraint on org_user_unique means the user is already a member — use ON CONFLICT DO NOTHING or treat as success
  2. Ensure orgOwnerRoleId is a valid org_roles.id (GetOrgOwnerRoleId result not stale)
  3. Check the transaction state — if WithTx already failed, fix the root error instead of the insert
  4. Verify org.Id/userId exist in orgs/users to rule out FK violations

Example fix

// before
query := "INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES ($1, $2, $3)"
// after
query := "INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT org_user_unique DO NOTHING"
Defensive patterns

Strategy: try-catch

Validate before calling

var exists bool
Conn.Get(&exists, "SELECT EXISTS(SELECT 1 FROM orgs_users WHERE org_id=$1 AND user_id=$2)", org.Id, userId)
// skip CreateOrgUser if exists

Try / catch

if err := CreateOrgUser(org.Id, userId, orgOwnerRoleId, tx); err != nil {
    if strings.Contains(err.Error(), "org_user_unique") {
        return org.Id, nil // already a member — treat as success
    }
    return "", fmt.Errorf("error adding org user: %v", err)
}

Prevention

When it happens

Trigger: During CreateAccount, the org matched by the user's email domain has AutoAddDomainUsers=true and CreateOrgUser's INSERT fails — typically org_user_unique duplicate (user already added concurrently or earlier), invalid org_owner role id FK, or DB error.

Common situations: User re-signs up or a retry double-adds them to the domain org; owner-role id lookup returned a stale/invalid value; transaction rolled back due to an earlier error in the same tx; Postgres constraint org_user_unique blocks the insert.

Related errors


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