Beego必须有一个名为`default`的注册数据库别名。

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

Beego must have one register DataBase alias named `default`

问题

在使用Beego的生产服务器中,我得到了一个错误信息:必须有一个名为default的注册数据库别名。

我知道在服务器上数据库连接凭据是有效的,但每当我进行RESTful请求时,我都会收到这个错误,并且Beego服务器会崩溃。

这是为什么会发生这种情况的原因吗?

以下是main.go中init函数内的代码:

orm.RegisterDriver("postgres", orm.DR_Postgres)

orm.RegisterDataBase("default", "postgres",
	fmt.Sprintf("postgres://%s:%s@%s/%s?port=%i",
		pgUser, pgPass, pgHost, pgDb, pgPort))
英文:

In the production server with Beego I get

must have one register DataBase alias named default

I know the db connection credentials work in the server but whenever I do restful request I get this error and the Beego server crashes.

Is there a reason why this is happening?

Below is the code inside main.go init function:

orm.RegisterDriver("postgres", orm.DR_Postgres)

orm.RegisterDataBase("default", "postgres",
	fmt.Sprintf("postgres://%s:%s@%s/%s?port=%i",
		pgUser, pgPass, pgHost, pgDb, pgPort))

答案1

得分: 1

你好!以下是你要翻译的内容:

根据你提供的错误信息,你可能没有使用orm.RegisterDataBase注册一个别名为default的数据库。下面是我从文档中找到的一个示例:

// 参数1:数据库别名,ORM将使用它来切换数据库。
// 参数2:驱动名称
// 参数3:连接字符串
orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8")

在beego中,通常在main.goinit函数中注册驱动和数据库(示例)。

英文:

Can you provide a sample of your code?

Based on the error message you have provided, you may not have registered a database with alias default using orm.RegisterDataBase. Here's an example which I have taken from the documentation:

// param 1:        Database alias. ORM will use it to switch database.
// param 2:        driverName
// param 3:        connection string
orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8")

In beego, it is common to register the driver and the database in init of main.go (example).

huangapple
  • 本文由 发表于 2015年1月12日 11:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/27894796.html
匿名

发表评论

匿名网友

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

确定