【问题标题】:How to specify a model to model association but without any dependency?如何指定模型到模型关联但没有任何依赖关系?
【发布时间】: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 中获得了嵌套的对象。

【问题讨论】:

    标签: go go-gorm


    【解决方案1】:

    我会亲自尝试这样做:

    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
    }
    

    【讨论】:

    • 第二个建议(带有指针的)应该可以。
    • 谢谢,完美。
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多