Can't use gorm with "show variables" phrase

Issue

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)

Solution

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"`
}{}

Answered By – serge-v

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.