{"record":{"id":"17d52269e5740f7a","repo":"pocketbase/pocketbase","slug":"no-collections-to-import","errorCode":null,"errorMessage":"no collections to import","messagePattern":"no collections to import","errorType":"validation","errorClass":null,"httpStatus":400,"severity":"error","filePath":"core/collection_import.go","lineNumber":39,"sourceCode":"\terr := json.Unmarshal(rawSliceOfMaps, &data)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn app.ImportCollections(data, deleteMissing)\n}\n\n// ImportCollections imports the provided collections data in a single transaction.\n//\n// For existing matching collections, the imported data is unmarshaled on top of the existing model.\n//\n// NB! If deleteMissing is true, ALL NON-SYSTEM COLLECTIONS AND SCHEMA FIELDS,\n// that are not present in the imported configuration, WILL BE DELETED\n// (this includes their related records data).\nfunc (app *BaseApp) ImportCollections(toImport []map[string]any, deleteMissing bool) error {\n\tif len(toImport) == 0 {\n\t\t// prevent accidentally deleting all collections\n\t\treturn errors.New(\"no collections to import\")\n\t}\n\n\timportedCollections := make([]*Collection, len(toImport))\n\tmappedImported := make(map[string]*Collection, len(toImport))\n\n\t// normalize imported collections data to ensure that all\n\t// collection fields are present and properly initialized\n\tfor i, data := range toImport {\n\t\tvar imported *Collection\n\n\t\tidentifier := cast.ToString(data[\"id\"])\n\t\tif identifier == \"\" {\n\t\t\tidentifier = cast.ToString(data[\"name\"])\n\t\t}\n\n\t\texisting, err := app.FindCollectionByNameOrId(identifier)\n\t\tif err != nil && !errors.Is(err, sql.ErrNoRows) {\n\t\t\treturn err","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/pocketbase/pocketbase/blob/5d217ddb50cb144d80a5d0b0bdf11b52b2c3e457/core/collection_import.go#L21-L57","documentation":"BaseApp.ImportCollections (core/collection_import.go:39) refuses to import an empty slice. This is an intentional safety guard: with deleteMissing=true the import would otherwise delete ALL non-system collections and their records, so an empty input is treated as an operator error rather than 'import nothing'.","triggerScenarios":"Calling ImportCollections([]map[string]any{}, ...) — via the Admin UI's import-collections dialog with an empty file, app.ImportCollections in Go code, or the /api/collections/import endpoint with an empty array body.","commonSituations":"A YAML/JSON migration file that parsed to zero collections (wrong path, empty fixture); CI importing a generated schema where the generator produced no output; reading a file whose frontmatter consumed everything.","solutions":["Pass a non-empty collections array in the import payload.","If the intent really was 'no collections', skip the import call entirely.","Check how the import file was loaded — an empty/failed read producing [] instead of an error will surface here."],"exampleFix":"// before\ndata, _ := os.ReadFile(\"schema.json\") // file missing -> empty\nvar cols []map[string]any\njson.Unmarshal(data, &cols)\napp.ImportCollections(cols, true) // -> no collections to import\n\n// after\ndata, err := os.ReadFile(\"schema.json\")\nif err != nil { return err }\nif err := json.Unmarshal(data, &cols); err != nil { return err }\nif len(cols) == 0 { return errors.New(\"schema.json contains no collections\") }\napp.ImportCollections(cols, true)","handlingStrategy":"validation","validationCode":"func importCollectionsSafe(app core.App, data []byte, deleteMissing bool) error {\n\tif len(bytes.TrimSpace(data)) == 0 {\n\t\treturn errors.New(\"import file is empty\")\n\t}\n\tvar cols []map[string]any\n\tif err := json.Unmarshal(data, &cols); err != nil {\n\t\treturn fmt.Errorf(\"invalid import json: %w\", err)\n\t}\n\tif len(cols) == 0 {\n\t\treturn errors.New(\"import payload contains no collections — refusing to run\")\n\t}\n\treturn app.ImportCollections(cols, deleteMissing)\n}","typeGuard":null,"tryCatchPattern":"if err := app.ImportCollections(cols, true); err != nil {\n  if strings.Contains(err.Error(), \"no collections to import\") {\n  \tlog.Warn(\"skipped import: payload was empty\")\n  \treturn nil // treat as a no-op, never as 'delete everything'\n  }\n  return err\n}","preventionTips":["Never build the import payload from a read that can silently produce an empty slice — fail on read/parse errors first.","Treat ImportCollections with deleteMissing=true as a destructive operation: diff and back up pb_data before running.","Add a unit test asserting len(toImport) > 0 in your migration harness."],"tags":["import","collections","schema","data-safety","pocketbase"],"backgroundTag":null,"analyzedSha":"5d217ddb50cb144d80a5d0b0bdf11b52b2c3e457","analyzedAt":"2026-08-15T10:06:33.165Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}