duanpin9531 2017-04-12 19:06
浏览 141

无法连接到Beego中的PostgreSQL数据库

I am playing around with Beego, but I stuck at connects to the PostgreSQL db.

Models

package models

import(
    "github.com/astaxie/beego/orm"  
    "time"
)

type User struct {
    Id   int
    Username string `orm:"size(100);unique"`
    Email string `orm:"size(64);unique"`
    Created time.Time `orm:"auto_now_add;type(datetime);index"`
    Edited time.Time `orm:"auto_now;type(datetime)"`
    Passpheres string 
    Activated bool   
}

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

And here is the main

package main

import (
    _ "test/routers"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/lib/pq"
    "test/models"
    "fmt"
)

func int(){
    orm.RegisterDriver("postgres", orm.DRPostgres)
    orm.RegisterDataBase("default", 
        "postgres",
        "user=aaaa password=1234567 host=127.0.0.1 port=5432 dbname=gotest sslmode=disable");

    orm.RunSyncdb("default", false, true)
}

func main() {
    orm.Debug = true

    o := orm.NewOrm()
    o.Using("default")
    u := new(models.User)
    u.Username = "test1"
    u.Email = "ads@asdad.com"
    u.Passpheres = "123456"
    u.Activated = true

     id, err := o.Insert(u)
     fmt.Printf("ID: %d, ERR: %v
", id, err)
    beego.Run()
}

And the log shows:

must have one register DataBase alias named default

I checked on the host and port of PostgreSQL and it is all correct, including the user, password and database.

Is this because of the db connection?

I am using PostgreSQL 9.5.6 and beego 1.8.1 by the way.

UPDATE How silly of me!! I just missed "i" in the init function declaration. So I just need to correct the syntax and and all good to go!

func init(){ // init instead of int
    orm.RegisterDriver("postgres", orm.DRPostgres)
    orm.RegisterDataBase("default", 
        "postgres",
        "user=aaaa password=1234567 host=127.0.0.1 port=5432 dbname=gotest sslmode=disable");

    orm.RunSyncdb("default", false, true)
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 shape_predictor_68_face_landmarks.dat
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制