【发布时间】:2022-01-20 17:47:38
【问题描述】:
我在 GORM 中有两个模型如下:
type (
Todo struct {
gorm.Model
EventId int64 `json:"event_id" form:"event_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
DoneUserId uint `json:"done_user_id" form:"done_user_id"`
Groups string `json:"groups" form:"groups"`
DoneAt *time.Time `json:"done_at" form:"done_at"`
TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
Priority uint `json:"priority" form:"priority" binding:"required"`
Done bool `json:"done" form:"done"`
Title string `json:"title" form:"title" binding:"required"`
Description string `json:"description" form:"description"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
Event Event
}
)
和
type (
Event struct {
gorm.Model
CustomerId uint `json:"customer_id" form:"customer_id" binding:"required"`
AddressId uint `json:"address_id" form:"address_id" binding:"required"`
UserId uint `json:"user_id" form:"user_id"`
EventType string `json:"event_type" form:"event_type" binding:"required"`
ContactPerson string `json:"contact_person" form:"contact_person"`
Title string `json:"title" form:"title" binding:"required"`
Description string `gorm:"type:text" json:"description" form:"description"`
Calculated bool `json:"calculated" form:"calculated"`
Goodwill bool `json:"goodwill" form:"goodwill"`
Billable bool `json:"billable" form:"billable"`
EventBegin time.Time `json:"event_begin" form:"event_begin" binding:"required"`
EventEnd time.Time `json:"event_end" form:"event_end" binding:"required"`
PartsJson string `gorm:"type:text" json:"parts_json" form:"parts_json"`
FieldsJson string `gorm:"type:text" json:"fields_json" form:"fields_json"`
CreatorId uint `json:"creator_id"`
ChangerId uint `json:"changer_id"`
Todos []Todo
Customer Customer
}
)
当我保存一个带有 event_id 集的新 Todo 时,我会收到很多关于事件对象中的空字段的错误。没错,因为我没有填充事件对象,所以我只是在 todo 对象中设置了 event_id。所以我的问题是,有没有办法可以禁用这些验证?
从 Todo 到 Event 的关联只是出于查询原因,我在 Todo-Object 中获得了一个嵌套的 Event-Object - 或者更好地说我在 json 中获得了嵌套的对象。
【问题讨论】: