无法使用gorm与”show variables”短语。

huangapple go评论109阅读模式
英文:

Can't use gorm with "show variables" phrase

问题

我正在尝试使用gorm检索mysql变量:

  1. sqlVars := []struct {
  2. Name string `db:"Variable_name"`
  3. Value int `db:"Value"`
  4. }{}
  5. res := sqlDB.Raw("show variables like 'max_connections'").Scan(&sqlVars)
  6. if res.Error != nil {
  7. log.Fatalf("failed to query MySQL parameter: %v", res.Error)
  8. }

当我执行这个查询时,只有Value字段被设置了,Name字段没有被设置。相同的结构在使用sqlx时工作正常。

有人知道是什么问题吗?

--

附上mysql输出:

  1. mysql> show variables like 'max_connections';
  2. +-----------------+-------+
  3. | Variable_name | Value |
  4. +-----------------+-------+
  5. | max_connections | 151 |
  6. +-----------------+-------+
  7. 1 row in set (0.00 sec)
英文:

I'm trying to retrieve mysql variables with gorm:

  1. sqlVars = []struct {
  2. Name string `db:"Variable_name"`
  3. Value int `db:"Value"`
  4. }{}
  5. res := sqlDB.Raw("show variables like 'max_connections'").Scan(&sqlVars)
  6. if res.Error != nil {
  7. log.Fatalf("failed to query MySQL parameter: %v", res.Error)
  8. }

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:

  1. mysql> show variables like 'max_connections';
  2. +-----------------+-------+
  3. | Variable_name | Value |
  4. +-----------------+-------+
  5. | max_connections | 151 |
  6. +-----------------+-------+
  7. 1 row in set (0.00 sec)

答案1

得分: 1

描述使用gorm标签的字段,如https://gorm.io/docs/models.html中所述。

示例:

  1. sqlVars := []struct {
  2. Name string `gorm:"column:Variable_name"`
  3. Value int `gorm:"column:Value"`
  4. }{}

请注意,gorm标签用于定义与数据库表列对应的字段名称。在上述示例中,Name字段对应数据库表的Variable_name列,Value字段对应数据库表的Value列。

英文:

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

Example:

  1. sqlVars := []struct {
  2. Name string `gorm:"column:Variable_name"`
  3. Value int `gorm:"column:Value"`
  4. }{}

huangapple
  • 本文由 发表于 2022年1月29日 19:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70905123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定