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

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

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
}

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:

确定