Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsupported data type in GORM

Tags:

go

go-gorm

I am currently exploring with GORM and have the following struct:

type Datasource struct {
    gorm.Model
    Inputs     []string               `gorm: serializer:json`
    Outputs    []string               `gorm: serializer:json`
    Parameters map[string]interface{} `gorm: serializer:json`
}

I do initialise my connection with the following func:

func ConnectDatabase() {

    database, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})

    if err != nil {
        panic("Failed to connect to database!")
    }

    err = database.AutoMigrate(&Datasource{})
    if err != nil {
        return
    }

    inputs := []string{"input_1", "input_2", "input_3"}
    outputs := []string{"output_1"}
    parameters := map[string]interface{}{"host": "here.it.is", "port": 8090}

    database.Create(&Datasource{Inputs: inputs, Outputs: outputs, Parameters: parameters})

    DB = database
}

but get the following error when executing AutoMigrate

[error] unsupported data type: &[]

Shall I use datatypes?

Thanks

like image 505
tog Avatar asked Oct 20 '25 02:10

tog


1 Answers

In fact the code above is almost working. GORM supports JSON natively but you have to proper annotate your code :-)

type Datasource struct {
    gorm.Model
    Inputs     []string               `gorm:"serializer:json"`
    Outputs    []string               `gorm:"serializer:json"`
    Parameters map[string]interface{} `gorm:"serializer:json"`
}

See this link for more details.

like image 95
tog Avatar answered Oct 22 '25 06:10

tog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!