【发布时间】:2020-05-22 08:58:12
【问题描述】:
我有一个 Go 结构,其中包含一段字符串,我想用 GORM 在 Postgres 中将其保存为 jsonB 对象。
我遇到了一个解决方案,它需要使用我想避免的 GORM 特定类型 (postgres.Jsonb)。
当我尝试在我的模型中使用切片运行 AutoMigrate 时,它会出现恐慌并且不会启动,尽管当我将此切片包装在一个结构中时(我可以这样做),它会运行而不会出错但不会在 postgres 中创建列。
type User struct {
gorm.Model
Data []string `sql:"type:"jsonb"; json:"data"`
} //Panics
type User struct {
gorm.Model
Data struct {
NestedData []string
} `sql:"type:"jsonb"; json:"data"`
} //Doesn't crash but doesn't create my column
有没有人能够在不使用模型中的 postgres.Jsonb 类型的情况下使用 GORM 操作 jsonb ?
【问题讨论】:
标签: postgresql go jsonb go-gorm