Query Beego problem

Asked

Viewed 75 times

0

I’m having problems to mount a Query in 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/cities.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 is coming back a err in Query, but as I am not treating is giving this error. What I do not understand is why return error in query? From what I understand it’s all right.

RESOLVED

You can’t use Querybuilder with Postgres, it has to be on Raw

  • Put the solution you found as an answer, or else I’m mistaken you have the option to answer your own question in this way you make it easy for other people to find the solution.

  • Show, now I answer

1 answer

2


In the Beego framework it is not possible to use the QueryBuilder with PgSql, with only MySql. So it was 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 the Query. In my view, it is not clear in the documentation that I can not use the QueryBuilder with PgSql. If anyone wants to take a look at the documentation where it talks about QueryBuilder here is the link: https://beego.me/docs/mvc/model/querybuilder.md

Browser other questions tagged

You are not signed in. Login or sign up in order to post.