From e4d637e6e0a8d06ee93035f3288502a584704b69 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sun, 12 Jan 2025 13:23:07 +0200 Subject: [PATCH] added test for partially matched table name/alias as suffix --- core/validators/db.go | 5 +++-- core/validators/db_test.go | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/validators/db.go b/core/validators/db.go index 2fd4f3b6..347ca89a 100644 --- a/core/validators/db.go +++ b/core/validators/db.go @@ -64,8 +64,9 @@ func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []stri normalizedErrs := validation.Errors{} for _, name := range fieldNames { - // note: extra space to exclude other fields starting with the current field name - if strings.Contains(msg, strings.ToLower(tableOrAlias+"."+name+" ")) { + // note: extra spaces to exclude table name with suffix matching the current one + // OR other fields starting with the current field name + if strings.Contains(msg, strings.ToLower(" "+tableOrAlias+"."+name+" ")) { normalizedErrs[name] = validation.NewError("validation_not_unique", "Value must be unique") } } diff --git a/core/validators/db_test.go b/core/validators/db_test.go index 9a761241..7ce2a9b4 100644 --- a/core/validators/db_test.go +++ b/core/validators/db_test.go @@ -78,6 +78,13 @@ func TestNormalizeUniqueIndexError(t *testing.T) { []string{"a", "b"}, nil, }, + { + "unique index error with table name suffix matching the specified one", + errors.New("UNIQUE constraint failed for fields test_suffix.a,test_suffix.b"), + "suffix", + []string{"a", "b", "c"}, + nil, + }, { "unique index error but mismatched fields", errors.New("UNIQUE constraint failed for fields test.a,test.b"),