From 4c9746c3be396e1db7a497654f5268a5e62f0aa8 Mon Sep 17 00:00:00 2001 From: Reza Date: Mon, 10 Jun 2024 13:55:17 +0200 Subject: [PATCH] fix unique validator --- lib/validation/validators.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/validation/validators.go b/lib/validation/validators.go index 3a4e14b..c2b76ec 100644 --- a/lib/validation/validators.go +++ b/lib/validation/validators.go @@ -1,6 +1,7 @@ package validation import ( + "context" "fmt" "github.com/getevo/evo/v2/lib/db" scm "github.com/getevo/evo/v2/lib/db/schema" @@ -72,8 +73,14 @@ func uniqueValidator(match []string, value *generic.Value, stmt *gorm.Statement, return nil } + of, zero := stmt.Schema.PrioritizedPrimaryField.ValueOf(context.Background(), reflect.ValueOf(stmt.Model)) + var c int64 - db.Where(field.DBName+" = ?", value.Input).Table(stmt.Table).Count(&c) + var model = db.Table(stmt.Table).Where(field.DBName+" = ?", value.Input) + if !zero { + model = model.Where(stmt.Schema.PrioritizedPrimaryField.DBName+" = ?", of) + } + model.Count(&c) if c > 0 { return fmt.Errorf("duplicate entry") }