From 76b901f115d600ef83bc804d242413cd3f52b4ee Mon Sep 17 00:00:00 2001 From: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Date: Tue, 30 May 2023 12:30:17 +0500 Subject: [PATCH 1/5] Added support of "Violates Foreign Key Constraint" --- error_translator.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/error_translator.go b/error_translator.go index 1f67d37..b611c66 100644 --- a/error_translator.go +++ b/error_translator.go @@ -8,8 +8,9 @@ import ( "github.com/jackc/pgx/v5/pgconn" ) -var errCodes = map[string]string{ - "uniqueConstraint": "23505", +var errDesToGormErrs = map[string]error{ + "23505": gorm.ErrDuplicatedKey, + "23503": gorm.ErrForeignKeyViolated, } type ErrMessage struct { @@ -22,8 +23,9 @@ type ErrMessage struct { // 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. func (dialector Dialector) Translate(err error) error { if pgErr, ok := err.(*pgconn.PgError); ok { - if pgErr.Code == errCodes["uniqueConstraint"] { - return gorm.ErrDuplicatedKey + gormErr, ok := errDesToGormErrs[pgErr.Code] + if ok { + return gormErr } return err } From fe5d465ef26c46aa24f8266db8221c50a37ce839 Mon Sep 17 00:00:00 2001 From: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Date: Tue, 30 May 2023 12:46:44 +0500 Subject: [PATCH 2/5] Added support of "Violates Foreign Key Constraint" --- error_translator.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/error_translator.go b/error_translator.go index b611c66..d40b06a 100644 --- a/error_translator.go +++ b/error_translator.go @@ -41,8 +41,9 @@ func (dialector Dialector) Translate(err error) error { return err } - if errMsg.Code == errCodes["uniqueConstraint"] { - return gorm.ErrDuplicatedKey - } + gormErr, ok := errDesToGormErrs[pgErr.Code] + if ok { + return gormErr + } return err } From 675d47a25bc7d64258fe8b0c5756ca761b88041e Mon Sep 17 00:00:00 2001 From: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Date: Tue, 30 May 2023 12:56:36 +0500 Subject: [PATCH 3/5] Added support of "Violates Foreign Key Constraint" --- error_translator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error_translator.go b/error_translator.go index d40b06a..65fdc25 100644 --- a/error_translator.go +++ b/error_translator.go @@ -41,7 +41,7 @@ func (dialector Dialector) Translate(err error) error { return err } - gormErr, ok := errDesToGormErrs[pgErr.Code] + gormErr, ok := errDesToGormErrs[errMsg.Code] if ok { return gormErr } From 27643a50e86879d3acb1569761a56f971217d2ac Mon Sep 17 00:00:00 2001 From: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Date: Tue, 30 May 2023 13:12:36 +0500 Subject: [PATCH 4/5] Update go.mod to include gorm.ErrForeignKeyViolated --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3a79ed1..24670c6 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/jackc/pgx/v5 v5.3.1 - gorm.io/gorm v1.25.0 + gorm.io/gorm v1.25.1 ) require ( From 86ebad496b567ae4cb8746fc335fd97ecd24fa6a Mon Sep 17 00:00:00 2001 From: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Date: Tue, 30 May 2023 13:13:51 +0500 Subject: [PATCH 5/5] Update go.sum to include gorm.ErrForeignKeyViolated --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index bb7edd7..9ae2c58 100644 --- a/go.sum +++ b/go.sum @@ -23,5 +23,5 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -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.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64= +gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=