为struct字段找到了无效字段,需要为关系定义外键,或者需要实现Valuer/Scanner接口 [英] invalid field found for struct field , need to define a foreign key for relations or it need to implement the Valuer/Scanner interface

查看:325
本文介绍了为struct字段找到了无效字段,需要为关系定义外键,或者需要实现Valuer/Scanner接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忽略不起作用.

检索错误:

发现无效的结构用于传递结构食品/模型餐厅的字段DeliveryZone,需要为关系定义外键,否则需要实施Valuer/Scanner界面

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

推荐答案

尝试

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

https://gorm.io/docs/models.html ->ctrl + F->忽略此字段

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

这篇关于为struct字段找到了无效字段,需要为关系定义外键,或者需要实现Valuer/Scanner接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆