0

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\n", 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)
}
Grokify
  • 15,092
  • 6
  • 60
  • 81
dev-jim
  • 2,404
  • 6
  • 35
  • 61
  • 2
    `RegisterDriver`, `RegisterDataBase`, and `RunSyncdb` all return error values, check those, if that doesn't help you make sure to include them in your question, maybe someone knows what they mean, that is, if they aren't nil. And by the way, if you aren't, please always do check errors. – mkopriva Apr 12 '17 at 19:14
  • It was my mistakes, I missed an 'i' in the main init function. Thanks for your suggestion by the way. – dev-jim Apr 13 '17 at 17:42
  • No problem, I'm glad you got it solved. – mkopriva Apr 13 '17 at 17:44
  • this is not even a question! – Alireza Fallah Mar 28 '22 at 11:16

0 Answers0