gogo-gorm

invalid field found for struct field , need to define a foreign key for relations or it need to implement the Valuer/Scanner interface


Omit not working.

Retrieve Error:

invalid field found for struct deliveryFood/models.Restaurant's field DeliveryZone, need to define a foreign key for relations or it need to implement the Valuer/Scanner interface

type Restaurant struct {
ID uint
Name string `json:"name"`
EmployeeId uint `json:"employee_id"`
Phone string `json:"phone"`
Address string `json:"address"`
ImagesUrl *string `json:"images_url"`
Position string `json:"position"`
WorkDays string `json:"work_days"`
StartWorkTime string `json:"start_work_time"`
EndWorkTime string `json:"end_work_time"`
Blocked bool `json:"blocked"`
DeliveryZone []*DeliveryZone `json:",omitempty"`
}

type DeliveryZone struct {
ID uint `json:"id"`
RestaurantId uint `json:"restaurant_id"`
Zone string `json:"zone"`
Price float32 `sql:"-"`
}
err := GetDB().Omit(clause.Associations).Model(Restaurant{}).Create(map[string]interface{} {
   "name": rest.Name,
   "EmployeeId": rest.EmployeeId,
   "Phone": rest.Phone,
   "Address": rest.Address,
   "ImagesUrl": rest.ImagesUrl,
   "WorkDays": rest.WorkDays,
   "StartWorkTime": rest.StartWorkTime,
   "EndWorkTime": rest.EndWorkTime,
   "Blocked": rest.Blocked,
   "Position": clause.Expr{
      SQL: "ST_GeomFromText(?)",
      Vars: []interface{}{fmt.Sprintf("POINT((%s))", rest.Position)},
   },
}).Error

Solution

  • try

    DeliveryZone []*DeliveryZone `gorm:"-"`
    

    https://gorm.io/docs/models.html -> ctrl+F -> ignore this field