Skip to content

Commit 18bc84b

Browse files
saeideeSaeid Saeidee
andauthored
feat: foreign key violated error (go-gorm#193)
* feat: foreign key violated error * ci: fixed ci workflow to be triggered for all pull requests * test: fixed typo --------- Co-authored-by: Saeid Saeidee <[email protected]>
1 parent 64c0de1 commit 18bc84b

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ name: tests
22

33
on:
44
push:
5-
branches:
6-
- '**'
7-
- '*'
85
pull_request:
9-
branches:
10-
- master
116

127
permissions:
138
contents: read

error_translator.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"github.com/jackc/pgx/v5/pgconn"
99
)
1010

11-
var errCodes = map[string]string{
12-
"uniqueConstraint": "23505",
11+
var errCodes = map[string]error{
12+
"23505": gorm.ErrDuplicatedKey,
13+
"23503": gorm.ErrForeignKeyViolated,
1314
}
1415

1516
type ErrMessage struct {
@@ -22,8 +23,8 @@ type ErrMessage struct {
2223
// Since currently gorm supporting both pgx and pg drivers, only checking for pgx PgError types is not enough for translating errors, so we have additional error json marshal fallback.
2324
func (dialector Dialector) Translate(err error) error {
2425
if pgErr, ok := err.(*pgconn.PgError); ok {
25-
if pgErr.Code == errCodes["uniqueConstraint"] {
26-
return gorm.ErrDuplicatedKey
26+
if translatedErr, found := errCodes[pgErr.Code]; found {
27+
return translatedErr
2728
}
2829
return err
2930
}
@@ -39,8 +40,8 @@ func (dialector Dialector) Translate(err error) error {
3940
return err
4041
}
4142

42-
if errMsg.Code == errCodes["uniqueConstraint"] {
43-
return gorm.ErrDuplicatedKey
43+
if translatedErr, found := errCodes[errMsg.Code]; found {
44+
return translatedErr
4445
}
4546
return err
4647
}

error_translator_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package postgres
2+
3+
import (
4+
"errors"
5+
"github.com/jackc/pgx/v5/pgconn"
6+
"gorm.io/gorm"
7+
"testing"
8+
)
9+
10+
func TestDialector_Translate(t *testing.T) {
11+
type fields struct {
12+
Config *Config
13+
}
14+
type args struct {
15+
err error
16+
}
17+
tests := []struct {
18+
name string
19+
fields fields
20+
args args
21+
want error
22+
}{
23+
{
24+
name: "it should return ErrDuplicatedKey error if the status code is 23505",
25+
args: args{err: &pgconn.PgError{Code: "23505"}},
26+
want: gorm.ErrDuplicatedKey,
27+
},
28+
{
29+
name: "it should return ErrForeignKeyViolated error if the status code is 23503",
30+
args: args{err: &pgconn.PgError{Code: "23503"}},
31+
want: gorm.ErrForeignKeyViolated,
32+
},
33+
}
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
dialector := Dialector{
37+
Config: tt.fields.Config,
38+
}
39+
if err := dialector.Translate(tt.args.err); !errors.Is(err, tt.want) {
40+
t.Errorf("Translate() expected error = %v, got error %v", err, tt.want)
41+
}
42+
})
43+
}
44+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/jackc/pgx/v5 v5.3.1
7-
gorm.io/gorm v1.25.0
7+
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
88
)
99

1010
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
2525
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2626
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
2727
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
28+
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
29+
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

0 commit comments

Comments
 (0)