【发布时间】:2020-05-07 07:04:32
【问题描述】:
我正在尝试保存一个结构数组。
我试过了:
type ShiftValue struct {
Hour uint8 `json:"hour"`
Minute uint8 `json:"minute"`
}
type Shift struct {
Start ShiftValue `json:"start"`
End ShiftValue `json:"end"`
}
type Config struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;index;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Shifts []Shift `gorm:"type:varchar(100)[];" json:"shifts,"`
}
但是不工作。我还尝试将 Shifts 保存为 pq.StringArray:
type Config struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;index;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Shifts pq.StringArray `gorm:"type:varchar(100)[];" json:"shifts,"`
}
这有点工作,但我不知道如何将Shift 的一部分转换为StringArray。
我应该使用GenericArrray吗?
如何将Slice 转换为GenericArray 或StringArray?
当我Unmarshall 数据时,我在以下结构中执行此操作,验证数据,然后将其保存到数据库:
type ConfigUpdate struct {
Shifts []Shift `json:"shifts,"`
}
【问题讨论】:
标签: postgresql go go-gorm