[英]sql: Scan error on column index 5, name "todos": unsupported Scan, storing driver.Value type []uint8 into type *[]datatypes.JSON golang gorm
This is the model:这是 model:
type Board struct {
Id uint `gorm:"primaryKey;autoIncrement;unique" json:"id"`
Owner uint `json:"owner"`
Name string `json:"name"`
Contributors []int `gorm:"type:jsonb" json:"contributors"`
GeneratedLink string `gorm:"default:''" json:"generated_link"`
Todos []datatypes.JSON `gorm:"type:jsonb" json:"todos"`
}
type TodoStructure struct {
Id string `json:"id"`
Text string `json:"text"`
Completed bool `json:"completed"`
Important bool `json:"important"`
}
This is how i create the board:这就是我创建董事会的方式:
board := models.Board{
Owner: 20,
Name: "text",
GeneratedLink: "",
Todos: []datatypes.JSON{datatypes.JSON("[]")},
}
database.DB.Create(&board)
This is how column looks after creating board:这是创建板后列的外观:
so todos is just an empty array.所以 todos 只是一个空数组。 And this is how i am creating todo:这就是我创建待办事项的方式:
func createTodo(c *gin.Context) {
var board models.board
database.DB.Where("id = ?", 1).First(&board)
id := uuid.NewV4().String()
todoStruct := models.TodoStructure{
Id: id,
Text: "text",
Completed: false,
Important: false,
}
jsonData, _ := json.Marshal(todoStruct)
board.Todos = append(board.Todos, jsonData)
fmt.Println(board.Todos) //exactly what i need [{"id":"e7ee015ab54c48388aa2413428f76f50","text":"text","completed":false,"important":false}]
database.DB.Save(&board)
c.JSON(http.StatusCreated, gin.H{"message": "success"})
}
but i got the error:但我得到了错误:
sql: Scan error on column index 5, name "todos": unsupported Scan, storing driver.Value type []uint8 into type *[]datatypes.JSON
and in column todo value saves like this:并在列 todo 值保存如下:
just json object, but i need to save like this:只是 json object,但我需要像这样保存:
[{"id":"e7ee015ab54c48388aa2413428f76f50","text":"text","completed":false,"important":false}]
Please any ideas, any options, i tried to find the solution in the google but not found right solution.请任何想法,任何选项,我试图在谷歌中找到解决方案,但没有找到正确的解决方案。
PS using gorm and postgresql PS 使用 gorm 和 postgresql
I think you use the incorrect data type.我认为您使用了不正确的数据类型。 Can you try this line?你可以试试这条线吗?
Todos []byte `gorm:"type:jsonb" json:"todos"`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.