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
Change RestaurantId to RestaurantID in DeliveryZone struct.
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:"-"`
}
Or you can define the foreignkey manually by adding foreignKey tag in Restaurant struct. e.g.
DeliveryZone []*DeliveryZone json:",omitempty" gorm:"foreignKey:RestaurantId"
try
DeliveryZone []*DeliveryZone `gorm:"-"`
https://gorm.io/docs/models.html -> ctrl+F -> ignore this field
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With