{"record":{"id":"02c4b455a354e1cb","repo":"plandex-ai/plandex","slug":"error-deleting-custom-models-v","errorCode":null,"errorMessage":"error deleting custom models: %v","messagePattern":"error deleting custom models: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/models.go","lineNumber":121,"sourceCode":"func GetCustomModel(orgId, id string) (*CustomModel, error) {\n\tvar model CustomModel\n\terr := Conn.Get(&model, `SELECT * FROM custom_models WHERE org_id = $1 AND id = $2`, orgId, id)\n\tif err != nil {\n\t\tif err == sql.ErrNoRows {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, err\n\t}\n\treturn &model, nil\n}\n\nfunc DeleteCustomModels(tx *sqlx.Tx, orgId string, ids []string) error {\n\tif tx == nil {\n\t\treturn fmt.Errorf(\"tx is nil\")\n\t}\n\t_, err := tx.Exec(`DELETE FROM custom_models WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error deleting custom models: %v\", err)\n\t}\n\treturn nil\n}\n\nfunc UpsertCustomProvider(tx *sqlx.Tx, p *CustomProvider) error {\n\tif tx == nil {\n\t\treturn fmt.Errorf(\"tx is nil\")\n\t}\n\tconst q = `\nINSERT INTO custom_providers (\n\t  org_id, name, base_url,\n\t  skip_auth, api_key_env_var, extra_auth_vars\n)\nVALUES (\n\t  $1,$2,$3,\n\t  $4,$5,$6\n)\nON CONFLICT (org_id, name)","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/models.go#L103-L139","documentation":"DeleteCustomModels wraps any failure of the batched DELETE on custom_models (scoped by org_id and id ANY($2)) with this message. It indicates the underlying PostgreSQL delete via sqlx Tx.Exec failed — not that zero rows were matched (deleting nothing is not an error). The wrapped %v is the driver/DB error (syntax, connection, constraint, or permission).","triggerScenarios":"Calling DeleteCustomModels with a tx whose connection is dead or already rolled back; ids array malformed for pq.Array; a FK constraint (e.g. model references) blocking the DELETE; missing DELETE privilege or table custom_models missing (migrations not applied).","commonSituations":"Migrations out of date so custom_models doesn't exist; caller passed a tx that failed earlier and was aborted (Postgres 'current transaction is aborted'); DB role lacking DELETE grant in production; transient connection drops mid-transaction.","solutions":["Inspect the wrapped error (%v) for the exact pq/pgerror code and check the table exists (\\d custom_models) / run migrations","If the tx was aborted by an earlier statement, fix the first failing statement or use a fresh tx per operation","Grant DELETE on custom_models to the app DB role","For FK violations, delete dependent rows first or add ON DELETE CASCADE","Retry once on transient connection errors before surfacing"],"exampleFix":"// before\n_, err := tx.Exec(`DELETE FROM custom_models WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))\n// after\nif len(ids) == 0 { return nil }\n_, err := tx.Exec(`DELETE FROM custom_models WHERE org_id = $1 AND id = ANY($2)`, orgId, pq.Array(ids))\nif err != nil {\n    var pgErr *pgconn.PgError\n    if errors.As(err, &pgErr) { log.Printf(\"delete custom_models failed: code=%s msg=%s\", pgErr.Code, pgErr.Message) }\n    return fmt.Errorf(\"error deleting custom models: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"func validateDeleteCustomModels(tx *sqlx.Tx, orgId string, ids []string) error {\n    if tx == nil { return errors.New(\"tx is nil\") }\n    if orgId == \"\" { return errors.New(\"orgId required\") }\n    for _, id := range ids { if id == \"\" { return errors.New(\"empty id\") } }\n    return nil\n}","typeGuard":"func isPgError(err error) (*pgconn.PgError, bool) {\n    var pgErr *pgconn.PgError\n    ok := errors.As(err, &pgErr)\n    return pgErr, ok\n}","tryCatchPattern":"if err := validateDeleteCustomModels(tx, orgId, ids); err != nil { return err }\nif err := db.DeleteCustomModels(tx, orgId, ids); err != nil {\n    var pgErr *pgconn.PgError\n    if errors.As(err, &pgErr) && (pgErr.Code == \"40001\" || pgErr.Code == \"40P01\") { return retryTx() }\n    return err\n}","preventionTips":["Always pass a live, non-nil transaction and check earlier statements in the same tx","Run migrations before deploy so custom_models exists","Grant DELETE on the table to the app role","Delete dependent rows first or use ON DELETE CASCADE","Log the wrapped pq error code, not just the wrapper message"],"tags":["database","postgres","sqlx","transaction"],"backgroundTag":"sql-delete-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}