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

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

Can't use gorm with "show variables" phrase

问题

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

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)
}

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

有人知道是什么问题吗?

--

附上mysql输出:

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

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)

答案1

得分: 1

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

示例:

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

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

英文:

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

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:

确定