【发布时间】:2020-02-19 05:31:11
【问题描述】:
我正在尝试使用 Gorm 从 mysql 进行简单的读取。
type Table struct {
Id string `json:"Id" db:"Id" column:"Id" gorm:"column:Id"`
}
func getTable(w http.ResponseWriter, r *http.Request) {
t:= []Table{}
db.Debug().Table("Table").Find(&t)
fmt.Println(table)
fmt.Println("len(table)")
}
MySql 有一个 table 列 id 作为主键
我得到的结果是一个大小正确的表格,但所有行都是空的......
....[{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}]
但是总行数是正确的len(t) = 20523
我认为我做错了在Table struct 的定义中,也许我错误地指定了Id 列的名称?任何建议表示赞赏。
【问题讨论】:
-
您的
gorm标签看起来不正确。这有帮助吗? gorm.io/docs/models.html -
Gorm怎么知道如何将MySql映射到我的Table struct -
我试过
Id stringjson:"Id" db:"Id" column:"Id" gorm:"type:varchar(20)"`` 还是一样的