duanpengya7074 2017-08-22 00:17
浏览 275
已采纳

如何使用beego / orm解决“没有可用的LastInsertId”

I am trying to use https://github.com/astaxie/beego/tree/master/orm to insert a struct into a postgres database. The operation should be simple

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

type Product struct {
    ID string `orm:"pk"`
    ...
}

product := &Product{ID: productID}
_, err := orm.NewOrm().Insert(product)
if err != nil {
    log.Fatal(err)
}

I keep getting this; no LastInsertId available whenever the code runs (the insert is otherwise successful) but I get a crash. I understand is it due to postgresql limitations because I use https://www.github.com/lib/pq driver.

Is there I way to work around this using beego/orm?

  • 写回答

1条回答 默认 最新

  • dongxiansi0158 2017-08-22 06:39
    关注

    If the crash is being caused by your log.Fatal(err), you can avoid this by checking and avoiding it:

    _, err := orm.NewOrm().Insert(product)
    
    if err != nil {
        if err.Error() == "no LastInsertId available" {
            log.Println(err)
        } else {
            log.Fatal(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效