user1478621
user1478621

Reputation: 11

beego QueryTable table name: `cpes` not exists

I have this pice of code in beego:

o := orm.NewOrm()
qs := o.QueryTable("cpes")

and I now that beego connects well with the database, and the database have the 'cpes' table, but I keep getting an error becouse beego don't find the table.

¿How can I debug this further?

Upvotes: 1

Views: 2813

Answers (3)

Ademu Anthoy
Ademu Anthoy

Reputation: 11

I had the same problem. In my case, it was because I didn't register the model.

orm.RegisterModel(new(Member), new(Bank), new(Queue), new(Payment))

Make sure you registered all your models with beego. The error message should have been more explicit though

Upvotes: 1

alienxt
alienxt

Reputation: 21

You must define model Cpes and register model 'cpes'.

Like:

type Cpes struct {
    Id int
}

func (u *Cpes) TableName() string {
    // db table name
    return "cpes"
}

func init() {
    orm.RegisterModel(new(Cpes))
}

Upvotes: 2

Diablojoe
Diablojoe

Reputation: 241

I had the same problem a few weeks ago. The answer is in how Beego is translating the table name in the ORM.

The quick fix is to use

qs := o.QueryTable(new(cpes))

Where cpes is the model struct.

If you want to see this in action or this solution does not work for you try using the bee generate api command on your database. This will give you the models in a pre-made fashion as well as a bunch of code examples on how to use them.

Best of luck!

Upvotes: 0

Related Questions