无法使用Beego的ORM的All()方法。

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

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

huangapple
  • 本文由 发表于 2013年9月27日 23:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/19054180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定