Home > Mobile >  Can't use gorm with "show variables" phrase
Can't use gorm with "show variables" phrase

Time:01-30

I'm trying to retrieve mysql variables with gorm:

sqlVars = []struct {
            Name  string `db:"Variable_name"`
            Value int    `db:"Value"`
          }{}

res := sqlDB.Raw("show variables like 'max_connections'").Scan(&sqlVars)
if res.Error != nil {
    log.Fatalf("failed to query MySQL parameter: %v", res.Error)
}

When I execute that query, only Value field is set, the Name field isn't. The same struct works fine with sqlx.

Does anyone know what's wrong?

--

Attached mysql output:

mysql> show variables like 'max_connections';
 ----------------- ------- 
| Variable_name   | Value |
 ----------------- ------- 
| max_connections | 151   |
 ----------------- ------- 
1 row in set (0.00 sec)

CodePudding user response:

Describe fields with gorm tag as described in https://gorm.io/docs/models.html

Example:

sqlVars := []struct {
        Name  string `gorm:"column:Variable_name"`
        Value int    `gorm:"column:Value"`
}{}
  •  Tags:  
  • Related