英文:
Unable to use the ORM .All() of Beego
问题
这是一段非常简单的代码,根据文档应该是可以工作的。
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"fmt"
)
type TestController struct {
beego.Controller
}
type Ticket struct {
Id int `orm:"auto"`
EventId int
EntryId int
}
func (this *TestController) Get() {
o := orm.NewOrm()
tickets := new([]*Ticket)
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(&tickets)
fmt.Print(qs)
this.Ctx.WriteString("test controller")
}
func init(){
orm.RegisterModel(new(Ticket))
}
这导致Beego崩溃,并显示以下错误:
GoEventKeeper:wrong object type `*[]*controllers.Ticket` for rows scan, need *[]*rohan.com/GoEventKeeper/controllers.Ticket or *rohan.com/GoEventKeeper/controllers.Ticket
我觉得这不应该发生,显然我只在controllers中有一个Ticket结构体,所以它似乎在将错误的值进行比较。
我需要做什么来解决这个问题?
英文:
Here's an extremely simple piece of code that should work according to the documentation.
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"fmt"
)
type TestController struct {
beego.Controller
}
type Ticket struct {
Id int `orm:"auto"`
EventId int
EntryId int
}
func (this *TestController) Get() {
o := orm.NewOrm()
tickets := new([]*Ticket)
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(&tickets)
fmt.Print(qs)
this.Ctx.WriteString("test controller")
}
func init(){
orm.RegisterModel(new(Ticket))
}
This results in Beego crashing with the following error:
GoEventKeeper:wrong object type `*[]*controllers.Ticket` for rows scan, need *[]*rohan.com/GoEventKeeper/controllers.Ticket or *rohan.com/GoEventKeeper/controllers.Ticket
I feel like this shouldn't be happening, obviously I only have one Ticket struct inside controllers so it seems to be comparing the wrong values with eachother?
What do i need to do to resolve this?
答案1
得分: 3
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(&tickets)
改为
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(tickets)
因为tickets已经是指针
更多详细信息请参考http://beego.me/docs/mvc/model/query.md#all
英文:
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(&tickets)
changes to
qs, _ := o.QueryTable(new(Ticket)).Filter("EventId", 2).All(tickets)
becasue tickets already is point
more detail please refer http://beego.me/docs/mvc/model/query.md#all
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论