plandex-ai/plandex · error

error deleting model pack: %v

Error message

error deleting model pack: %v

What it means

DeleteModelPacks wraps a failure of the batched DELETE on model_sets (org_id + id ANY($2)). Deleting zero matching rows is not an error; this fires only when the statement execution fails, e.g. FK constraints from tables referencing model_sets, permissions, or an aborted transaction.

Source

Thrown at app/server/db/models.go:269

	return modelPacks, nil
}

func ListModelPacksForNames(orgId string, names []string) ([]*ModelPack, error) {
	var modelPacks []*ModelPack
	query := `SELECT * FROM model_sets WHERE org_id = $1 AND name = ANY($2)`
	err := Conn.Select(&modelPacks, query, orgId, names)
	return modelPacks, err
}

func DeleteModelPacks(tx *sqlx.Tx, orgId string, ids []string) error {
	if tx == nil {
		return fmt.Errorf("tx is nil")
	}
	_, err := tx.Exec(`DELETE FROM model_sets WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))

	if err != nil {
		return fmt.Errorf("error deleting model pack: %v", err)
	}

	return nil
}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Inspect the wrapped pq error code: 23503 means FK violation — delete or cascade dependent rows first
  2. If the tx is aborted, roll back and retry in a fresh tx
  3. Grant DELETE on model_sets to the app role
  4. Confirm model_sets exists via migrations
  5. Retry once on transient connection/deadlock errors

Example fix

// before
_, err := tx.Exec(`DELETE FROM model_sets WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))
// after
_, err := tx.Exec(`DELETE FROM custom_models WHERE model_set_id = ANY($1)`, pq.Array(ids)) // clear dependents
if err != nil { return err }
_, err = tx.Exec(`DELETE FROM model_sets WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))
Defensive patterns

Strategy: try-catch

Validate before calling

// before deleting, ensure no dependents
var refs int
err := tx.Get(&refs, `SELECT count(*) FROM custom_models WHERE model_set_id = ANY($1)`, pq.Array(ids))
if err == nil && refs > 0 { return errors.New("model pack still referenced by custom models") }

Type guard

func isPgCode(err error, code string) bool {
    var pgErr *pgconn.PgError
    return errors.As(err, &pgErr) && pgErr.Code == code
}

Try / catch

if err := db.DeleteModelPacks(tx, orgId, ids); err != nil {
    if isPgCode(err, "23503") { return fmt.Errorf("pack referenced by other rows: %w", err) }
    return err
}

Prevention

When it happens

Trigger: tx already aborted by an earlier failed statement; model rows referencing the model_set block the DELETE with a foreign-key violation; missing DELETE grant; model_sets table missing.

Common situations: Dependent custom models still reference the pack; shared production DB where app role lacks DELETE; caller reusing a failed tx in a multi-step handler.

Related errors


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