Beego must have one register DataBase alias named

2019-08-09 00:23发布

问题:

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



标签: go beego