'Go-gorm mysql "unsupported type []string, a slice of string"
I use gorm, and try to create transaction to mysql. I have a struct
type Game struct {
Images []string
}
game := Game{Images: []string{"1.png", "2.png"}}
db := Database()
tx := db.Begin()
if err := tx.Create(&game).Error; err != nil {
tx.Rollback()
return errors.New("Cannot add game")
}
tx.Commit()
But I get error (sql: converting argument $1 type: unsupported type []string, a slice of string). I understand, that mysql unsupported this type, but can I someway resolve this problem? I guess I can change type to json.rawMessage, but I think it's a wrong way.
I use dialect "github.com/jinzhu/gorm/dialects/mysql"
Solution 1:[1]
You can use datatypes.JSON as your type
import "gorm.io/datatypes"
type Game struct {
Images datatypes.JSON `json:"images"`
}
reference: https://github.com/go-gorm/datatypes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hayreenfly |
