plandex-ai/plandex · error

error creating org: %v

Error message

error creating org: %v

What it means

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.

Source

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

func CreateOrg(req *shared.CreateOrgRequest, userId string, domain *string, tx *sqlx.Tx) (*Org, error) {
	org := &Org{
		Name:               req.Name,
		Domain:             domain,
		AutoAddDomainUsers: req.AutoAddDomainUsers,
		OwnerId:            userId,
	}

	err := 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)

	if err != nil {
		if IsNonUniqueErr(err) {
			// Handle the uniqueness constraint violation
			return nil, fmt.Errorf("an org with domain %s already exists", *domain)

		}

		return nil, fmt.Errorf("error creating org: %v", err)
	}

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

	_, err = tx.Exec("INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES ($1, $2, $3)", org.Id, userId, orgOwnerRoleId)

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

	return org, nil
}

func GetOrgForDomain(domain string) (*Org, error) {
	var org Org

View on GitHub (pinned to e2d772072e)

Solutions

  1. Read the wrapped %v message and map it to the exact Postgres error code (e.g. 23502 not-null, 42501 permission)
  2. Validate req.Name is non-empty and within length limits before calling CreateOrg
  3. Ensure migrations are up to date so the orgs schema matches the code
  4. Check the transaction's prior statements — if tx was already aborted, fix the earlier error rather than this one

Example fix

// before
req := &shared.CreateOrgRequest{Name: ""}
org, err := db.CreateOrg(req, userId, &domain, tx)
// after
if req.Name == "" || len(req.Name) > 100 {
    return errors.New("org name is required (max 100 chars)")
}
org, err := db.CreateOrg(req, userId, &domain, tx)
Defensive patterns

Strategy: validation

Validate before calling

if req.Name == "" || len(req.Name) > 100 { return errors.New("invalid org name") }
if tx == nil { return errors.New("CreateOrg requires a transaction") }

Type guard

func isCreateOrgErr(err error) bool { return strings.HasPrefix(err.Error(), "error creating org: ") }

Try / catch

org, err := db.CreateOrg(req, userId, &domain, tx)
if err != nil {
    log.Printf("create org failed: %v", err) // includes wrapped pq error
    return fmt.Errorf("could not create organization")
}

Prevention

When it happens

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

Common situations: 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.

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/dacaca72529b768f. Report an issue: GitHub.