如何在没有任何依赖关系的情况下指定模型到模型的关联



我在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中得到了嵌套的对象。

我个人会尝试这样做:

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
}

不确定它是否适用于戈尔姆。否则,我可能会看到将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
}

相关内容

最新更新