Skip to content

Commit

Permalink
feat: foreign key violatation error (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
Co-authored-by: Jinzhu <wosmvp@gmail.com>
  • Loading branch information
3 people committed Jun 7, 2023
1 parent 86ba643 commit cb7f946
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions error_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"gorm.io/gorm"
)

var errCodes = map[string]int{
"uniqueConstraint": 2067,
var errCodes = map[int]error{
2067: gorm.ErrDuplicatedKey,
768: gorm.ErrForeignKeyViolated,
}

type ErrMessage struct {
Expand All @@ -30,8 +31,8 @@ func (dialector Dialector) Translate(err error) error {
return err
}

if errMsg.ExtendedCode == errCodes["uniqueConstraint"] {
return gorm.ErrDuplicatedKey
if translatedErr, found := errCodes[errMsg.ExtendedCode]; found {
return translatedErr
}
return err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/mattn/go-sqlite3 v1.14.17
gorm.io/gorm v1.25.0
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

0 comments on commit cb7f946

Please sign in to comment.