Code Monkey home page Code Monkey logo

Comments (6)

vmihailenco avatar vmihailenco commented on July 22, 2024

That looks like a bug. Fixing.

from pg.

vmihailenco avatar vmihailenco commented on July 22, 2024

Please try latest version. The issue should be fixed. Thanks for the report.

Also note that v3 does not require Scan and Value methods any more. I.e. following should work too:

package main

import (
    "fmt"

    "gopkg.in/pg.v3"
)

func pgdb() *pg.DB {
    return pg.Connect(&pg.Options{
        User:     "postgres",
        Database: "test",
    })
}

func main() {
    db := pgdb()

    var user User
    _, err := db.QueryOne(&user, `SELECT '{"timezone":"mytimezone"}'::jsonb AS settings`)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n", user.Settings)
}

type User struct {
    Settings *UserJsonb // it's a jsonb field in pg
}

type UserJsonb struct {
    Timezone    string
    Stockparams []float64
}

from pg.

leedstyh avatar leedstyh commented on July 22, 2024

Thanks for the quick fixing.

BUT as to v3 does not require Scan and Value, I have new issue:

Read from db works well, but update db will panic.

Error message is panic: pg: unsupported src type: <main.UserJsonb Value>

package main

import (
    "fmt"
    "time"
    "strings"
    // "database/sql/driver"
    // "encoding/json"

    "gopkg.in/pg.v3"
)

func pgdb() *pg.DB {
    return pg.Connect(&pg.Options{
        User:     "pg",
    })
}

func main() {
    db := pgdb()

    user := User{Nickname:"Leeds"}
    //if no Scan() and Value(), read from db is ok
    _, err := db.QueryOne(&user, "SELECT * FROM tbl_user WHERE LOWER(nickname) = ?", strings.ToLower(user.Nickname))
    if err != nil {
        panic(err)
    }
    fmt.Println(user.Nickname, user.Settings)

    user.Settings.Stockparams[0]++

    fmt.Println("always works well above")

    err = user.UpdateSettings() //if no Scan() and Value(), update db will panic
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println(user.Nickname, user.Settings)
    }

}

type User struct {
    Id         int
    Uid        string
    Nickname   string
    Password   string
    Salt       string
    Email      string
    Createtime time.Time
    Settings   *UserJsonb
}

func (self *User) UpdateSettings() error {
    db := pgdb()

    sql := "UPDATE tbl_user SET settings = ?settings WHERE nickname = ?nickname"
    _, err := db.ExecOne(sql, self)
    return err
}

type UserJsonb struct {
    Timezone    string
    Stockparams []float64
}

// func (m *UserJsonb) Scan(value interface{}) error {
//  return json.Unmarshal(value.([]byte), m)
// }

// func (m UserJsonb) Value() (driver.Value, error) {
//  b, err := json.Marshal(m)
//  if err != nil {
//      return nil, err
//  }
//  return string(b), nil
// }

from pg.

leedstyh avatar leedstyh commented on July 22, 2024

Full panic stack"

panic: pg: unsupported src type: <main.UserJsonb Value>

goroutine 1 [running]:
gopkg.in/pg%2ev3.appendStructValue(0xc208086000, 0x24, 0x2000, 0x6518e0, 0xc20803a960, 0xd9, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/append_value.go:69 +0x2a6
gopkg.in/pg%2ev3.appendValue(0xc208086000, 0x24, 0x2000, 0x6518e0, 0xc20803a960, 0xd9, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/append.go:117 +0x2fd
gopkg.in/pg%2ev3.appendValue(0xc208086000, 0x24, 0x2000, 0x5d6cc0, 0xc20803a960, 0x16, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/append.go:114 +0x272
gopkg.in/pg%2ev3.appendIface(0xc208086000, 0x24, 0x2000, 0x5d6cc0, 0xc20803a960, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/append.go:104 +0x835
gopkg.in/pg%2ev3._appendValue(0xc208086000, 0x24, 0x2000, 0x5d6cc0, 0xc208054070, 0xd6, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/types.go:25 +0xa3
gopkg.in/pg%2ev3.(*pgValue).AppendValue(0xc20802e500, 0xc208086000, 0x24, 0x2000, 0x671fa0, 0xc208054000, 0xd9, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/types.go:109 +0x257
gopkg.in/pg%2ev3.formatQuery(0xc208086000, 0x24, 0x2000, 0xc20802e320, 0x43, 0x50, 0xc208077e00, 0x0, 0x1, 0x0, ...)
    /home/leeds/code/src/gopkg.in/pg.v3/append.go:399 +0x968
gopkg.in/pg%2ev3.AppendQ(0xc208086000, 0x5, 0x2000, 0x6efe90, 0x43, 0xc208077e00, 0x1, 0x1, 0x0, 0x0, ...)
    /home/leeds/code/src/gopkg.in/pg.v3/append.go:16 +0xe7
gopkg.in/pg%2ev3.writeQueryMsg(0xc20802a000, 0x6efe90, 0x43, 0xc208077e00, 0x1, 0x1, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/messages.go:137 +0xb8
gopkg.in/pg%2ev3.simpleQuery(0xc2080300c0, 0x6efe90, 0x43, 0xc208077e00, 0x1, 0x1, 0x10, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/db.go:380 +0x7f
gopkg.in/pg%2ev3.(*DB).Exec(0xc20800abb0, 0x6efe90, 0x43, 0xc208077e00, 0x1, 0x1, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/db.go:195 +0xe5
gopkg.in/pg%2ev3.(*DB).ExecOne(0xc20800abb0, 0x6efe90, 0x43, 0xc208077e00, 0x1, 0x1, 0x0, 0x0, 0x0)
    /home/leeds/code/src/gopkg.in/pg.v3/db.go:208 +0x78
main.(*User).UpdateSettings(0xc208054000, 0x0, 0x0)
    /home/leeds/code/src/app/pg.go:59 +0x11e
main.main()
    /home/leeds/code/src/app/pg.go:35 +0x468

goroutine 5 [chan receive]:
github.com/golang/glog.(*loggingT).flushDaemon(0x7da280)
    /home/leeds/code/src/github.com/golang/glog/glog.go:879 +0x78
created by github.com/golang/glog.init·1
    /home/leeds/code/src/github.com/golang/glog/glog.go:410 +0x2a7

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

from pg.

vmihailenco avatar vmihailenco commented on July 22, 2024

Should be fixed. Please try again.

from pg.

leedstyh avatar leedstyh commented on July 22, 2024

Yes, works well now. Nice job!

from pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.