简体   繁体   English

sql:列索引 5 上的扫描错误,名称“todos”:不支持扫描,存储驱动程序。值类型 []uint8 到类型 *[]datatypes.JSON golang gorm

[英]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.

相关问题 Swift错误“无法下标类型[Uint8]的值” - Swift error “Cannot subscript a value of type [Uint8]” 为什么会出现错误:从类型'UINT8 *'分配给类型'P_Name_t'时类型不兼容 - Why is there an error: incompatible types when assigning to type 'P_Name_t' from type 'UINT8 *' 在Python中将一个大整数截断为uint8类型 - Truncate a big integer to a uint8 type in Python 无法使用 uint8 类型初始化数组 - Can't init array with uint8 type 不能使用类型为“UInt32”的索引为 [String] 类型的值添加下标 - Cannot subscript a value of type [String] with an index of type “UInt32” 在Golang中,为什么这样的类型转换会导致运行时错误:索引超出范围? - In Golang, why such Type Conversion causes Runtime Error: index out of range? TypeError:返回参数类型元组(int_const 23,bool,uint8 [3]内存)不可转换为预期类型元组(uint256,bool,uint256 [3]内存) - TypeError: Return argument type tuple(int_const 23,bool,uint8[3] memory) is not convertible to expected type tuple(uint256,bool,uint256[3] memory) 在 GoLang 中将字节切片“[]uint8”转换为 float64 - Convert byte slice “[]uint8” to float64 in GoLang 如何扫描数组中的空索引,并在php中使用新值打印? - how to scan null index in array, and print it with a new value in php? C函数来扫描和打印任何基本数据类型数组 - C functions to scan and print any basic data type array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM