panic: <Ormer> table: .
not found, make sure it was registered with RegisterModel()
今天在用beego的orm进行数据查询的时候的时候出现了一个错误。
相关代码如下:
models/files.go
package models
import (
"github.com/astaxie/beego/orm"
"time"
)
type FileInfo struct {
Id int // 主键
Filename string `json:"filename" orm:"column(filename);size(255);"`
UploadTime time.Time `json:"uploadtime" orm:"column(uploadtime);auto_now_add;type(datetime)"`
Size int
FileType string `json:"filetype" orm:"column(filetype);size(255);"`
}
func init() {
orm.RegisterModel(new(FileInfo))
}
upload/test.go
package upload
import (
"fmt"
"bary/distribution/models"
"github.com/astaxie/beego/orm"
"time"
)
func Test() {
o := orm.NewOrm()
o.Using("default")
var file = new(models.FileInfo)
file.Filename = "cmds.go"
err := o.Read(&file, "Filename")
fmt.Println(err)
}
main.go
package main
import (
_ "bary/distribution/routers"
"github.com/astaxie/beego/orm"
_ "bary/distribution/models"
_ "github.com/go-sql-driver/mysql"
"bary/distribution/upload"
)
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:password@tcp(127.0.0.1:3306)/test")
orm.RunSyncdb("default", false, true)
}
func main() {
upload.Test()
}
上网查询这个错误:
基本上都说原因是没有有RegisterModel。
比如: https://github.com/astaxie/beego/issues/1672
但我明明注册过了。
后来找了半天发现是:
err := o.Read(&file, "Filename")
这个语句有问题,应该改成
err := o.Read(file, "Filename")
真是可怕的问题。。
网友评论