Beego must have one register DataBase alias named `default`

亡梦爱人 提交于 2019-12-11 05:42:39

问题


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:


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).



来源:https://stackoverflow.com/questions/27894796/beego-must-have-one-register-database-alias-named-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!