在代码中怎么判断 Duplicate Entry error ?
Originally posted by @Jimwi in https://github.com/go-gorm/gorm/issues/3821#issuecomment-1050138287
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: Heliner
gorm not support in the return args error Error,
but you can do something temporarily,
when the create model but primary key is unique , I use user.id as primary key, when i insert two model which id both 1 , get an error “UNIQUE constraint failed: users.id”,so
// xxxxxx
err := db.Create(User).Error
errStr:= err.Error()
if strings.HasPrefix(errStr, "UNIQUE constraint failed:") {
// add your self code when get db depliute key error str
}else if ... other error{
}
// xxxx
you may cause db depliute key error like this : https://github.com/go-gorm/playground/runs/5522344797?check_suite_focus=true
and the error will return like this:
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: xdxiaodong
import mysql2 "github.com/go-sql-driver/mysql"
var (
// ErrDuplicateEntryCode 命中唯一索引
ErrDuplicateEntryCode = 1062
)
// MysqlErrCode 根据mysql错误信息返回错误代码
func MysqlErrCode(err error) int {
mysqlErr, ok := err.(*mysql2.MySQLError)
if !ok {
return 0
}
return int(mysqlErr.Number)
}
if db.MysqlErrCode(err) == db.ErrDuplicateEntryCode {
}
上面的代码是你要的
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: Jimwi
```go import mysql2 "github.com/go-sql-driver/mysql"
var ( // ErrDuplicateEntryCode 命中唯一索引 ErrDuplicateEntryCode = 1062 )
// MysqlErrCode 根据mysql错误信息返回错误代码 func MysqlErrCode(err error) int { mysqlErr, ok := err.(*mysql2.MySQLError) if !ok { return 0 } return int(mysqlErr.Number) }
if db.MysqlErrCode(err) == db.ErrDuplicateEntryCode {
} ```
上面的代码是你要的
thanks
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨