- avoid log 1062(duplicate key) mysql error

This commit is contained in:
gazebo
2018-10-24 09:51:26 +08:00
parent aa0b574a35
commit f050ec926b

View File

@@ -5,7 +5,9 @@ import (
"reflect"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego/orm"
"github.com/go-sql-driver/mysql"
)
const (
@@ -130,10 +132,14 @@ func CreateEntity(db *DaoDB, item interface{}) (err error) {
if db == nil {
db = GetDB()
}
err = utils.CallFuncLogError(func() error {
_, err = db.db.Insert(item) // todo 这里需要将ID赋值么
return err
}, reflect.TypeOf(item).Name())
if _, err = db.db.Insert(item); err != nil {
if mysqlErr, ok := err.(*mysql.MySQLError); ok {
if mysqlErr.Number == 1062 {
return err
}
}
globals.SugarLogger.Warnf("CreateEntity failed with error:%v", err)
}
return err
}