Skip to content

Commit

Permalink
fix unique validator
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Jun 10, 2024
1 parent bacb230 commit 4c9746c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/validation/validators.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 4c9746c

Please sign in to comment.