英文:
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对象中得到一个嵌套的Event对象,或者更准确地说,我希望在json中得到嵌套的对象。
英文:
i got two models in GORM as following:
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
}
)
and
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
}
)
When I save a new Todo with an event_id set, then I get many errors regarding empty fields within the event object. Thats right, because I do not fill the event object I just set the event_id in the todo object. So my question is, is there a way how I can disable these validations?
The association from Todo to Event is just for query reasons, that I get a nested Event-Object in Todo-Object - or better said I get the nested object in the json.
答案1
得分: 2
我个人会这样尝试实现:
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
}
希望能对你有所帮助!
英文:
I would personally try do it like this:
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
}
Not sure if it'll work with Gorm. Otherwise, I'd probably see how much work it would be to just change Event to a pointer:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论