Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: map mysql error 1451 to gorm error #145

Merged
merged 1 commit into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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