Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary key data type #7

Closed
cmhobbs opened this issue Aug 3, 2012 · 3 comments
Closed

Primary key data type #7

cmhobbs opened this issue Aug 3, 2012 · 3 comments

Comments

@cmhobbs
Copy link

cmhobbs commented Aug 3, 2012

Can the primary key be established as anything other than int? I'd like to be able to store an md5 string as the primary key for one of my tables. I have a struct that amounts to something like this:

type MyThing struct {
        Md5         string `PK`
        Description string
}

I'm storing it thusly:

func storething() {
        db, err := sql.Open("postgres", "user=someuser password=somepass dbname=thingdb")
        if err != nil {
                panic(err)
        }

        var thing MyThing
        thing.Md5 = "5c19ed618b82f97de0fd349d00431fb2"
        thing.Description = "my hovercraft is full of eels"

        orm := beedb.New(db, "pg")
        beedb.OnDebug = true
        orm.Save(&thing)
}

Calling storething() results in the following error:

2012/08/02 22:41:53 panic: reflect: call of reflect.Value.Int on string Value
/home/hobbsc/go/src/pkg/github.com/astaxie/beedb/beedb.go:360 (0x42909b)
        com/astaxie/beedb.(*Model).Save: if reflect.ValueOf(id).Int() == 0 {

----  further stack trace goes here ----

When I set the primary key as type int (be it id or some other field), this works just fine.

@astaxie
Copy link
Owner

astaxie commented Aug 12, 2012

i found it is a bug, thanks, i will fix it next week

@astaxie
Copy link
Owner

astaxie commented Aug 13, 2012

i think this problem today, i find some problems
orm.Save(&thing)
i have to distinguish wherther it is insert or update
if you set a string pk,then if you use save function maybe always update.And if i support string then insert return LastInsertId will error. So now it's hard to support Primary key for other data type

@astaxie astaxie closed this as completed Aug 13, 2012
@chinakr
Copy link

chinakr commented Apr 27, 2013

我也遇到了类似的错误。如果没有if err != nil { orm = initDb() }这部分代码,就有可能出现reflect: call of reflect.Value.Set on zero Value beedb这个错误。如果有这部分代码,就不会出错了,但是原因我还没想明白。谢大能给看一下吗?

// 把微博数据保存到数据库
func saveWeibos(apiWeibos ApiWeiboList) {
    orm := initDb()
    for _, apiWeibo := range(apiWeibos.Statuses) {
        weibo := Weibo{}
        err := orm.Where("wid=?", apiWeibo.Id).Find(&weibo)    // 更新或新建记录
        //Check(err)
        if err != nil {    // No record found
            orm = initDb()
        }
        weibo.Wid = apiWeibo.Id
        weibo.Text = apiWeibo.Text
        weibo.Created = getTime(apiWeibo.Created_At)
        err = orm.Save(&weibo)
        Check(err)
        Debug("Weibo %d(%v) saved.", weibo.Wid, weibo.Created)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants