Skip to content

Commit

Permalink
feat: map mysql error 1451 to gorm error (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaostudy committed Mar 9, 2024
1 parent b87f024 commit 739f97b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions error_translator.go
Expand Up @@ -9,6 +9,7 @@ import (
// The error codes to map mysql errors to gorm errors, here is the mysql error codes reference https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html.
var errCodes = map[uint16]error{
1062: gorm.ErrDuplicatedKey,
1451: gorm.ErrForeignKeyViolated,
1452: gorm.ErrForeignKeyViolated,
}

Expand Down
5 changes: 5 additions & 0 deletions error_translator_test.go
Expand Up @@ -29,6 +29,11 @@ func TestDialector_Translate(t *testing.T) {
args: args{err: &mysql.MySQLError{Number: uint16(1062)}},
want: gorm.ErrDuplicatedKey,
},
{
name: "it should translate error to ErrForeignKeyViolated when the error number is 1451",
args: args{err: &mysql.MySQLError{Number: uint16(1451)}},
want: gorm.ErrForeignKeyViolated,
},
{
name: "it should translate error to ErrForeignKeyViolated when the error number is 1452",
args: args{err: &mysql.MySQLError{Number: uint16(1452)}},
Expand Down

0 comments on commit 739f97b

Please sign in to comment.