I have a Task
type that has a list of Runner
type objects in it. I am trying to map it to database using golang gorm but it doesn't have foreign key and i am getting invalid association
during migration
My Task struct:
type Task struct {
gorm.Model
Name string `gorm:"not null;unique_index"`
Description string
Runners []Runner
}
My Runner struct:
type Runner struct {
gorm.Model
Name string `gorm:"not null;unique"`
Description string
}
My migration code:
func migrateSchema () (err error) {
db, err := context.DBProvider()
if err != nil {
return
}
db.Model(&Task{}).Related(&Runner{})
db.AutoMigrate(&Task{})
db.AutoMigrate(&Runner{})
return
}
On db.AutoMigrate(&Task{})
I get invalid association
message in console and when I check the database there is no foreign key created or no reference field created on runners
table
What am I doing wrong?