Defining foreign key relationships in Go using GORM

Viewed 12

I have the following three models

type Client struct {
    gorm.Model
    Name    string `json:"name"`
    Address string `json:"address"`
    Phone   string `json:"phone"`
    Email   string `json:"email"`
}
type Item struct {
    gorm.Model
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    Image       string  `json:"image"`
}
type Offer struct {
    gorm.Model
    Date    string `json:"date"`
    To      Client `json:"to"`
    Title   string `json:"title"`
    Content string `json:"content"`
    Items   []Item `json:"items"`
}

An offer is made to a client, indicating one-to-many relationship - since there can be multiple offers made to one client, an offer contains items, which indicates many-to-many relatonship, since any offer can have one or more items

Digging through the docs, I tried the following:

type Offer struct {
    gorm.Model
    Date    string `json:"date"`
    To      Client `json:"to" gorm:"foreignKey:ClientID"`
    Title   string `json:"title"`
    Content string `json:"content"`
    Items   []Item `json:"items" gorm:"many2many:offers_items;"`
}

But when I'm trying to run the migrations like so:

err = db.AutoMigrate(&Client{}, &Item{}, &Offer{})
if err != nil {
    panic("Could not complete auto migrations")
}

I'm receiving this trace:

[error] invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface

[error] failed to parse value &model.Offer{Model:gorm.Model{ID:0x0, CreatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:gorm.DeletedAt{Time:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), Valid:false}}, Date:"", To:model.Client{Model:gorm.Model{ID:0x0, CreatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:gorm.DeletedAt{Time:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), Valid:false}}, Name:"", Address:"", Phone:"", Email:""}, Title:"", Content:"", Items:[]model.Item(nil)}, got error invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface

[error] invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface

[error] invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface

[error] failed to parse value &model.Offer{Model:gorm.Model{ID:0x0, CreatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:gorm.DeletedAt{Time:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), Valid:false}}, Date:"", To:model.Client{Model:gorm.Model{ID:0x0, CreatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:gorm.DeletedAt{Time:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), Valid:false}}, Name:"", Address:"", Phone:"", Email:""}, Title:"", Content:"", Items:[]model.Item(nil)}, got error invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface

[error] invalid field found for struct github.com/orshemtov/offers-system/server/pkg/model.Offer's field To: define a valid foreign key for relations or implement the Valuer/Scanner interface
panic: Could not complete auto migrations

exit status 2

Environment:

  • Go 1.19
  • gorm.io/driver/mysql v1.3.6
  • gorm.io/gorm v1.23.9
  • MySQL running in Docker
0 Answers
Related