关联者所属qor admin

I have these two structures:

type Collection struct {
    gorm.Model
    APIKey       string
    CollectionID string
    Name         string
    Environments []Environment
}
type Environment struct {
    gorm.Model
    EnvironmentID string
    Name          string
    Provider      string
    FlightType    string
    ADT           int
    CHD           int
    INF           int
}

And the main looks like:

func main() {
    adminResource := admin.New(&admin.AdminConfig{DB: model.DB})
    adminResource.AddResource(&model.Collection{})
    adminResource.AddResource(&model.Environment{})

    mux := http.NewServeMux()
    adminResource.MountTo("/admin", mux)

    if err := http.ListenAndServe(":8000", mux); err != nil {
        panic(err)
    } else {
        fmt.Println("Listening on: 8000")
    }
}

The point is that I don´t know if I should to do something else to allow the membership relationship one-to-many between the Collection and the Environemt. The thing is that the form on the admin view looks good, I can add as many environments as I was... but submitting the form only the Collection are saved on the database.

Finally I found the solution.

type Collection struct {
    gorm.Model
    APIKey       string
    CollectionID string
    Name         string
    Environments []Environment
}
type Environment struct {
    gorm.Model
    EnvironmentID string
    Name          string
    Provider      string
    FlightType    string
    ADT           int
    CHD           int
    INF           int
    CollectionID  int
}

Adding CollectionID int on the Environment struct is enough... so simply :D.

Thanks @apxp

The thing, which is missing is to tell gorm the foreign key of the other model.

In your case we use Has Many (gorm Doc). To define the relationship you have to add a tag to your struct:

type Collection struct {
    gorm.Model
    APIKey       string
    CollectionID string
    Name         string
    Environments []Environment `gorm:"foreignkey:EnvironmentID"`
}
type Environment struct {
    gorm.Model
    EnvironmentID string
    Name          string
    Provider      string
    FlightType    string
    ADT           int
    CHD           int
    INF           int
}

Without defining the foreign key of the other model gorm is not able to match those both models. As the convention for the primary key is ID and your Enviroment does not have that field it is not possible to match something. Be sure to read the documentation about the conventions.