{"record":{"id":"889776d7fdba0802","repo":"plandex-ai/plandex","slug":"error-deleting-model-packs-w","errorCode":null,"errorMessage":"error deleting model packs: %w","messagePattern":"error deleting model packs: %w","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/models.go","lineNumber":323,"sourceCode":"\t\t\t\treturn fmt.Errorf(\"error creating model pack: %w\", err)\n\t\t\t}\n\t\t}\n\n\t\tif len(toDeleteCustomModelIds) > 0 {\n\t\t\tif err := db.DeleteCustomModels(tx, auth.OrgId, toDeleteCustomModelIds); err != nil {\n\t\t\t\treturn fmt.Errorf(\"error deleting custom models: %w\", err)\n\t\t\t}\n\t\t}\n\n\t\tif len(toDeleteCustomProviderIds) > 0 {\n\t\t\tif err := db.DeleteCustomProviders(tx, auth.OrgId, toDeleteCustomProviderIds); err != nil {\n\t\t\t\treturn fmt.Errorf(\"error deleting custom providers: %w\", err)\n\t\t\t}\n\t\t}\n\n\t\tif len(toDeleteModelPackIds) > 0 {\n\t\t\tif err := db.DeleteModelPacks(tx, auth.OrgId, toDeleteModelPackIds); err != nil {\n\t\t\t\treturn fmt.Errorf(\"error deleting model packs: %w\", err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n\n\tif err != nil {\n\t\tlog.Printf(\"Error: %v\\n\", err)\n\t\thttp.Error(w, \"Failed to import custom models/providers/model packs: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tw.WriteHeader(http.StatusOK)\n\n\tlog.Println(\"Successfully imported custom models/providers/model packs\")\n}\n\nfunc GetCustomModelHandler(w http.ResponseWriter, r *http.Request) {","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/models.go#L305-L341","documentation":"Wrapped error raised in the org model-import transaction when db.DeleteModelPacks fails while removing org model packs whose names are absent from the submitted config. The WithTx closure aborts, rolling back the entire import, and the handler returns 500.","triggerScenarios":"POST of a custom model config where existing org model packs are omitted from the payload and db.DeleteModelPacks(tx, auth.OrgId, toDeleteModelPackIds) returns a DB error (FK constraint from plans/default settings referencing the pack, connection failure, deadlock).","commonSituations":"A plan or org default settings still points at the model pack being removed; renaming a pack in config so the old one is treated as deleted; DB connectivity issues.","solutions":["Repoint or delete any plan/org settings that reference the model pack before removing it from the config","Read the wrapped error in server logs to identify the failing constraint","Retry the import once the reference is removed; the rollback keeps prior state consistent","Check DB health/pool if the underlying error is a connection issue"],"exampleFix":"// before: pack deleted while still the org default\nconfig.ModelPacks = otherPacks\n// after: set default elsewhere or keep the pack\nconfig.ModelPacks = otherPacks\nsetDefaultPlanConfigForOrg(\"gpt-4-pack\")","handlingStrategy":"validation","validationCode":"// don't drop a pack that is the org/user default or referenced by plans\nconst packNames = new Set(config.ModelPacks?.map(p => p.Name) ?? [])\nif (defaultPackName && !packNames.has(defaultPackName)) {\n  throw new Error('cannot delete default model pack ' + defaultPackName)\n}","typeGuard":"function keepsDefaultPack(config, defaultPackName) {\n  const names = new Set((config.ModelPacks ?? []).map(p => p.Name))\n  return names.has(defaultPackName)\n}","tryCatchPattern":"try {\n  await client.ImportCustomModels(config)\n} catch (err) {\n  if (String(err.message).includes('error deleting model packs')) {\n    // repoint plans/org defaults away from the pack, then retry\n  }\n}","preventionTips":["Repoint org/plan default settings before removing a model pack from config","Rename packs deliberately — a rename is treated as delete+create in this flow","Retry after failure; the transactional rollback keeps prior packs intact"],"tags":["database","transaction","foreign-key"],"backgroundTag":"db-transaction-aborted","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}