如何指定模型之间的关联但没有任何依赖关系?

huangapple go评论113阅读模式
英文:

How to specify a model to model association but without any dependency?

问题

我在GORM中有两个模型,如下所示:

  1. type (
  2. Todo struct {
  3. gorm.Model
  4. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  5. UserId uint `json:"user_id" form:"user_id"`
  6. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  7. Groups string `json:"groups" form:"groups"`
  8. DoneAt *time.Time `json:"done_at" form:"done_at"`
  9. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  10. Priority uint `json:"priority" form:"priority" binding:"required"`
  11. Done bool `json:"done" form:"done"`
  12. Title string `json:"title" form:"title" binding:"required"`
  13. Description string `json:"description" form:"description"`
  14. CreatorId uint `json:"creator_id"`
  15. ChangerId uint `json:"changer_id"`
  16. Event Event
  17. }
  18. )
  19. type (
  20. Event struct {
  21. gorm.Model
  22. CustomerId uint `json:"customer_id" form:"customer_id" binding:"required"`
  23. AddressId uint `json:"address_id" form:"address_id" binding:"required"`
  24. UserId uint `json:"user_id" form:"user_id"`
  25. EventType string `json:"event_type" form:"event_type" binding:"required"`
  26. ContactPerson string `json:"contact_person" form:"contact_person"`
  27. Title string `json:"title" form:"title" binding:"required"`
  28. Description string `gorm:"type:text" json:"description" form:"description"`
  29. Calculated bool `json:"calculated" form:"calculated"`
  30. Goodwill bool `json:"goodwill" form:"goodwill"`
  31. Billable bool `json:"billable" form:"billable"`
  32. EventBegin time.Time `json:"event_begin" form:"event_begin" binding:"required"`
  33. EventEnd time.Time `json:"event_end" form:"event_end" binding:"required"`
  34. PartsJson string `gorm:"type:text" json:"parts_json" form:"parts_json"`
  35. FieldsJson string `gorm:"type:text" json:"fields_json" form:"fields_json"`
  36. CreatorId uint `json:"creator_id"`
  37. ChangerId uint `json:"changer_id"`
  38. Todos []Todo
  39. Customer Customer
  40. }
  41. )

当我保存一个设置了event_id的新Todo时,我会收到许多关于事件对象中空字段的错误。这是正确的,因为我没有填充事件对象,我只是在todo对象中设置了event_id。所以我的问题是,有没有办法可以禁用这些验证?

从Todo到Event的关联只是为了查询目的,我希望在Todo对象中得到一个嵌套的Event对象,或者更准确地说,我希望在json中得到嵌套的对象。

英文:

i got two models in GORM as following:

  1. type (
  2. Todo struct {
  3. gorm.Model
  4. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  5. UserId uint `json:"user_id" form:"user_id"`
  6. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  7. Groups string `json:"groups" form:"groups"`
  8. DoneAt *time.Time `json:"done_at" form:"done_at"`
  9. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  10. Priority uint `json:"priority" form:"priority" binding:"required"`
  11. Done bool `json:"done" form:"done"`
  12. Title string `json:"title" form:"title" binding:"required"`
  13. Description string `json:"description" form:"description"`
  14. CreatorId uint `json:"creator_id"`
  15. ChangerId uint `json:"changer_id"`
  16. Event Event
  17. }
  18. )

and

  1. type (
  2. Event struct {
  3. gorm.Model
  4. CustomerId uint `json:"customer_id" form:"customer_id" binding:"required"`
  5. AddressId uint `json:"address_id" form:"address_id" binding:"required"`
  6. UserId uint `json:"user_id" form:"user_id"`
  7. EventType string `json:"event_type" form:"event_type" binding:"required"`
  8. ContactPerson string `json:"contact_person" form:"contact_person"`
  9. Title string `json:"title" form:"title" binding:"required"`
  10. Description string `gorm:"type:text" json:"description" form:"description"`
  11. Calculated bool `json:"calculated" form:"calculated"`
  12. Goodwill bool `json:"goodwill" form:"goodwill"`
  13. Billable bool `json:"billable" form:"billable"`
  14. EventBegin time.Time `json:"event_begin" form:"event_begin" binding:"required"`
  15. EventEnd time.Time `json:"event_end" form:"event_end" binding:"required"`
  16. PartsJson string `gorm:"type:text" json:"parts_json" form:"parts_json"`
  17. FieldsJson string `gorm:"type:text" json:"fields_json" form:"fields_json"`
  18. CreatorId uint `json:"creator_id"`
  19. ChangerId uint `json:"changer_id"`
  20. Todos []Todo
  21. Customer Customer
  22. }
  23. )

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

我个人会这样尝试实现:

  1. type MinimalTodo struct {
  2. gorm.Model
  3. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  4. UserId uint `json:"user_id" form:"user_id"`
  5. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  6. Groups string `json:"groups" form:"groups"`
  7. DoneAt *time.Time `json:"done_at" form:"done_at"`
  8. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  9. Priority uint `json:"priority" form:"priority" binding:"required"`
  10. Done bool `json:"done" form:"done"`
  11. Title string `json:"title" form:"title" binding:"required"`
  12. Description string `json:"description" form:"description"`
  13. CreatorId uint `json:"creator_id"`
  14. ChangerId uint `json:"changer_id"`
  15. }
  16. func (MinimalTodo) TableName() string {
  17. return "todos"
  18. }
  19. type Todo struct {
  20. MinimalTodo
  21. Event
  22. }

不确定它是否适用于 Gorm。否则,我可能会看看将 Event 改为指针需要多少工作:

  1. type Todo struct {
  2. gorm.Model
  3. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  4. UserId uint `json:"user_id" form:"user_id"`
  5. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  6. Groups string `json:"groups" form:"groups"`
  7. DoneAt *time.Time `json:"done_at" form:"done_at"`
  8. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  9. Priority uint `json:"priority" form:"priority" binding:"required"`
  10. Done bool `json:"done" form:"done"`
  11. Title string `json:"title" form:"title" binding:"required"`
  12. Description string `json:"description" form:"description"`
  13. CreatorId uint `json:"creator_id"`
  14. ChangerId uint `json:"changer_id"`
  15. Event *Event
  16. }

希望能对你有所帮助!

英文:

I would personally try do it like this:

  1. type MinimalTodo struct {
  2. gorm.Model
  3. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  4. UserId uint `json:"user_id" form:"user_id"`
  5. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  6. Groups string `json:"groups" form:"groups"`
  7. DoneAt *time.Time `json:"done_at" form:"done_at"`
  8. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  9. Priority uint `json:"priority" form:"priority" binding:"required"`
  10. Done bool `json:"done" form:"done"`
  11. Title string `json:"title" form:"title" binding:"required"`
  12. Description string `json:"description" form:"description"`
  13. CreatorId uint `json:"creator_id"`
  14. ChangerId uint `json:"changer_id"`
  15. }
  16. func (MinimalTodo) TableName() string {
  17. return "todos"
  18. }
  19. type Todo struct {
  20. MinimalTodo
  21. Event
  22. }

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:

  1. type Todo struct {
  2. gorm.Model
  3. EventId int64 `json:"event_id" form:"event_id" binding:"required"`
  4. UserId uint `json:"user_id" form:"user_id"`
  5. DoneUserId uint `json:"done_user_id" form:"done_user_id"`
  6. Groups string `json:"groups" form:"groups"`
  7. DoneAt *time.Time `json:"done_at" form:"done_at"`
  8. TodoEnd time.Time `json:"todo_end" form:"todo_end" binding:"required"`
  9. Priority uint `json:"priority" form:"priority" binding:"required"`
  10. Done bool `json:"done" form:"done"`
  11. Title string `json:"title" form:"title" binding:"required"`
  12. Description string `json:"description" form:"description"`
  13. CreatorId uint `json:"creator_id"`
  14. ChangerId uint `json:"changer_id"`
  15. Event *Event
  16. }

huangapple
  • 本文由 发表于 2021年12月18日 06:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/70399304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定