diff --git a/CHANGELOG.md b/CHANGELOG.md index bc5c3e6c..90f205d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## v0.24.3 (WIP) +- Fixed incorrectly reported unique validator error for fields starting with name of another field (#6281; thanks @svobol13). + - Reload the created/edited records data in the RecordsPicker UI. - Updated Go dependencies. diff --git a/core/validators/db.go b/core/validators/db.go index bf437b1d..2fd4f3b6 100644 --- a/core/validators/db.go +++ b/core/validators/db.go @@ -50,7 +50,6 @@ func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []stri return err } - // if _, ok := err.(validation.Errors); ok { return err } @@ -59,12 +58,14 @@ func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []stri // check for unique constraint failure if strings.Contains(msg, "unique constraint failed") { + // note: extra space to unify multi-columns lookup + msg = strings.ReplaceAll(strings.TrimSpace(msg), ",", " ") + " " + normalizedErrs := validation.Errors{} - msg = strings.ReplaceAll(strings.TrimSpace(msg), ",", " ") for _, name := range fieldNames { - // blank space to unify multi-columns lookup - if strings.Contains(msg+" ", strings.ToLower(tableOrAlias+"."+name)) { + // note: extra space to exclude 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 f5ba1f26..9a761241 100644 --- a/core/validators/db_test.go +++ b/core/validators/db_test.go @@ -92,6 +92,13 @@ func TestNormalizeUniqueIndexError(t *testing.T) { []string{"a", "b", "c"}, []string{"a", "b"}, }, + { + "unique index error with matching table name and field starting with the name of another non-unique field", + errors.New("UNIQUE constraint failed for fields test.a_2,test.c"), + "test", + []string{"a", "a_2", "c"}, + []string{"a_2", "c"}, + }, } for _, s := range scenarios {