Problem Query Beego

0

I'm having trouble setting up a Query on Go, using the Beego framework.

Error:

  

2018/12/20 09: 21: 32.104 [C] [asm_amd64.s: 522] Handler crashed with error runtime error: invalid memory address or nil pointer dereference   2018/12/20 09: 21: 32.104 [C] [asm_amd64.s: 522] C: /Go/src/runtime/asm_amd64.s: 522   2018/12/20 09: 21: 32.104 [C] [asm_amd64.s: 522] C: /Go/src/runtime/panic.go: 513   2018/12/20 09: 21: 32.104 [C] [asm_amd64.s: 522] C: /Go/src/runtime/panic.go: 82   2018/12/20 09: 21: 32.104 [C] [asm_amd64.s: 522] C: /Go/src/runtime/signal_windows.go: 204   2018/12/20 09: 21: 32.105 [C] [asm_amd64.s: 522] C: /Users/joao/go/src/hello/controllers/cidades.go: 35   2018/12/20 09: 21: 32.109 [C] [asm_amd64.s: 522] C: /Users/joao/go/src/github.com/astaxie/beego/router.go: 834   2018/12/20 09: 21: 32.110 [C] [asm_amd64.s: 522] C: /Go/src/net/http/server.go: 2741   2018/12/20 09: 21: 32.110 [C] [asm_amd64.s: 522] C: /Go/src/net/http/server.go: 1847   2018/12/20 09: 21: 32.111 [C] [asm_amd64.s: 522] C: /Go/src/runtime/asm_amd64.s: 1333   2018/12/20 09: 21: 32.111 [server.go: 2977] [HTTP] http: multiple response.WriteHeader calls

Controller:

package controllers

import (
    "hello/models"

    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/lib/pq"
)

type CidadesController struct {
    beego.Controller
}

func init() {
    beego.BConfig.WebConfig.AutoRender = false
    orm.RegisterDriver("postgres", orm.DRPostgres)
    orm.RegisterDataBase("default", "User", "postgres:senha@/nomeDoDB? 
    charset=utf8", 30)
}

func (this *CidadesController) Get() {

    var cidade []models.Cidade

    cidades, err := orm.NewQueryBuilder("postgres")

    if err != nil {
        //
    }

    cidades.Select("cid_codigo",
        "cid_nome").
        From("ger_cidade").
        Limit(10).
        Offset(0)

    sql := cidades.String()

    o := orm.NewOrm()
    o.Raw(sql, 20).QueryRows(&cidade)
}

Model:

package models

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

type Cidade struct {
    Id   int    'db:"cid_codigo"'
    Name string 'db:"cid_nome"'
}

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

From what I understand it is returning a err in Query, but as I am not dealing with it is giving this error. What I do not understand is because it returns error in the query? From what I can understand, it's all right.

RESOLVED

You can not use QueryBuilder with Postgres, it has to be Raw

    
asked by anonymous 20.12.2018 / 12:33

1 answer

2

In the Beego framework it is not possible to use QueryBuilder with PgSql , only with MySql . Then it looks like this:

No Controller:

package controllers

import (
    "fmt"
    "hello/models"

    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/lib/pq"
)

type CidadesController struct {
    beego.Controller
}

func init() {
    beego.BConfig.WebConfig.AutoRender = false
    orm.RegisterDriver("postgres", orm.DRPostgres)
    orm.RegisterDataBase("default", "postgres", "dbname=nomeDoDB host=localhost 
user=user password=senhaDoDb port=5432 sslmode=disable", 30)
}

func (this *CidadesController) Get() {

    o := orm.NewOrm()
    o.Using("default")

    var cidade []models.Cidade

    res, err := o.Raw("SELECT cid_codigo as id, cid_nome, est_codigo FROM 
    geral.ger_cidade").QueryRows(&cidade)

    if err != nil {
        fmt.Println(err)
        return
    }

    if res == 0 {
        fmt.Println("Sem registros")
        return
    }

    this.Data["json"] = &cidade
    this.ServeJSON()
}

No Model:

package models

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

type Cidade struct {
    Id         int    'json:"id" db:"nomeDoCampoNoDB"'
    Cid_nome   string 'json:"name" db:"nomeDoCampoNoDB"'
    Est_codigo int    'json:"codigo_estado" db:"nomeDoCampoNoDB"'
}

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

I used sql "pure" to do Query. In my point of view, it is not quite explicit in the documentation that I can not use QueryBuilder with PgSql . If anyone wants to take a look at the documentation where they talk about QueryBuilder here is the link: link

    
21.12.2018 / 18:54