I'm trying to use the native JSON support introduced in MySQL 5.7.8
Below is a quick sample app I'm trying out.
DB Schema
CREATE TABLE IF NOT EXISTS `experiment` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL DEFAULT '',
`age` INT(10) NOT NULL DEFAULT 10,
`content` JSON NOT NULL,
PRIMARY KEY (`id`)
)ENGINE = InnoDB DEFAULT CHARSET=latin1;
GO code
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"encoding/json"
)
type Experiment struct {
gorm.Model
Name string `json:"name"`
Age int64 `json:"age"`
Content map[string]interface{} `json:"content"`
}
func main() {
db, err := gorm.Open("mysql", "root:root@/testdb?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
j1 := []byte(`{"name":"anu", "age": 123, "content": { "k1" : "v1", "k2" : [1,2,3]}}`)
var ex Experiment
json.Unmarshal(j1, &ex)
// Create
db.Create(&ex)
}
Here Experiment.Content values are unpredictable. I have to use a generic map for that reasons.
However This program throws the following error (sql: converting Exec argument $6 type: unsupported type map[string]interface {}, a map)
Is this not supported yet with the go-gorm library. What's the best way to achieve both read and write for this json content?