返回正确的列数,但所有行都是空的
Returning the right number of columns, but all rows are empty
我正在尝试使用 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
为 primary_key
我得到的结果是 table,大小正确,但所有行都是空的...
....[{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}]
但是总行数是正确的len(t) = 20523
我认为我在 Table struct
的定义中做错了,也许我错误地指定了 Id 列的名称?任何建议表示赞赏。
您可以使用 db.Raw 并检查字段是否具有名称 "Id",如下所示:
type Table struct {
Id string `json:"Id" db:"Id" column:"Id" gorm:"column:Id"`
}
func getTable() {
t := []Table{}
db := config.ConnectDB()
defer db.Close()
db.Raw(" SELECT idEvaluacionAnual as Id FROM EvaluacionesAnuales ").Scan(&t)
fmt.Println(t.Id)
fmt.Println("len(table)")
}
希望对您有所帮助
我正在尝试使用 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
为 primary_key
我得到的结果是 table,大小正确,但所有行都是空的...
....[{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}]
但是总行数是正确的len(t) = 20523
我认为我在 Table struct
的定义中做错了,也许我错误地指定了 Id 列的名称?任何建议表示赞赏。
您可以使用 db.Raw 并检查字段是否具有名称 "Id",如下所示:
type Table struct {
Id string `json:"Id" db:"Id" column:"Id" gorm:"column:Id"`
}
func getTable() {
t := []Table{}
db := config.ConnectDB()
defer db.Close()
db.Raw(" SELECT idEvaluacionAnual as Id FROM EvaluacionesAnuales ").Scan(&t)
fmt.Println(t.Id)
fmt.Println("len(table)")
}
希望对您有所帮助