I want to create collections using a migration script an running go run . migrate for pocketbase
Expected: a collection is created with all properties
Got: a collection is created but it only has 1 property id
My Code
From main.go
package main
import (
"log"
"os"
"strings"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
_ "myapp/migrations" // REQUIRED to run migrations
)
func main() {
app := pocketbase.New()
// Register the migrate command + automigrate
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
Automigrate: true, // true to automigrate
})
if err := app.Start(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
I tried two methods to import collections
- import from JSON
app.ImportCollectionsByMarshaledJSON([]byte(rawJSON), false); - import from map
app.ImportCollections(collections, false);
The rawJSON from above looks like this
[{
"name": "products",
"type": "base",
"schema": [
{"name": "name", "type": "text", "required": true, "options": {}},
{"name": "sku", "type": "text", "required": true, "unique": true, "options": {}},
{"name": "price", "type": "number", "required": true, "options": {}},
{"name": "description", "type": "text", "required": false, "options": {}},
{"name": "category", "type": "relation", "required": false,
"options": {"collectionId":"categories","cascadeDelete":false,"maxSelect":1}}
]
}]
The code to import a collection using a map is this
func init() {
m.Register(func(app core.App) error {
// add up queries...
return upMigrationTwo(app)
}, func(app core.App) error {
// add down queries...
return nil
})
}
and upMigrationTwo(app) is this
func upMigrationTwo(app core.App) error {
collections := []map[string]any{
{
"name": "customers", "type": "base",
"schema": []map[string]any{
{"name":"name", "type":"text","required": true, "options": map[string]any{},},
{"name":"email","type":"email","required": true,"unique": true,
"options": map[string]any{},},
},
},
{
"name": "orders", "type": "base",
"schema": []map[string]any{
{"name": "customerId","type": "relation","required": true,
"options": map[string]any{ "collectionId": "customers",
"cascadeDelete": false, "maxSelect": 1,},
},
{"name":"total", "type":"number","required": true,
"options": map[string]any{},},
},
},
}
if err := app.ImportCollections(collections, false); err != nil {
return err
}
return nil
}
If i run go run . migrate the console output confirms that a migration was run - for example Applied 00003_products.go for the json import. But the collections only have the id attribute

What must be done to import a collection / to migrate collections from code?