1

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.

Eloy Fernández Franco
  • 1,350
  • 1
  • 24
  • 47
  • Did you set any relations between your two models? https://godoc.org/github.com/jinzhu/gorm#Relationship – apxp Aug 10 '18 at 07:25
  • Hi @apxp I was trying to do this: `db.Model(&Collection).Related(&Environment)`. I tried to use the structs of Collection and Environments as arguments, but these functions are oriented to the instance. And **I don´t know where to do that**, because the handling of the forms is managed internally by the qor. Is there any way to add a middleware (or something) on the Collection´s load?? – Eloy Fernández Franco Aug 10 '18 at 08:20

2 Answers2

1

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.

apxp
  • 5,240
  • 4
  • 23
  • 43
  • Hi again @apxp It seems that starts to work... but I found a pair of new problems: 1) The admin doesn´t allow edit the EnvironmentID (and it is needed). The solution: `Environments []Environment \`gorm:"foreignkey:ID"\``. 2) This solution generates a new problem: Only the last Environment is saved on the DB, trying to add a new one, this last edits the previous, instead insert a new env. 3) I try to do this (on the env struct): `ID uint \`gorm:"primary_key" sql:"AUTO_INCREMENT"\`` But adding more than env I get: _pq: duplicate key violates uniqueness restriction «environments_pkey»_ – Eloy Fernández Franco Aug 11 '18 at 09:46
0

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

Eloy Fernández Franco
  • 1,350
  • 1
  • 24
  • 47