如何指定一个模型来模型关联但没有任何依赖?
How to specify a model to model association but without any dependency?
我在 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.[=12= 中得到了嵌套对象]
我个人会尝试这样做:
type MinimalTodo 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"`
}
func (MinimalTodo) TableName() string {
return "todos"
}
type Todo struct {
MinimalTodo
Event
}
不确定它是否适用于 Gorm。否则,我可能会看到将 Event 更改为指针需要做多少工作:
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
}
我在 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.[=12= 中得到了嵌套对象]
我个人会尝试这样做:
type MinimalTodo 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"`
}
func (MinimalTodo) TableName() string {
return "todos"
}
type Todo struct {
MinimalTodo
Event
}
不确定它是否适用于 Gorm。否则,我可能会看到将 Event 更改为指针需要做多少工作:
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
}