From 45e804dd3fa3ca11fc3db0945fc3c4b93e8b7e66 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 29 Nov 2021 16:19:06 +0800 Subject: [PATCH] Fix call valuer interface when using nil value --- clause/expression.go | 2 +- statement.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/clause/expression.go b/clause/expression.go index d0498306..dde00b1d 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -368,7 +368,7 @@ func (like Like) NegationBuild(builder Builder) { } func eqNil(value interface{}) bool { - if valuer, ok := value.(driver.Valuer); ok { + if valuer, ok := value.(driver.Valuer); ok && !eqNilReflect(valuer) { value, _ = valuer.Value() } diff --git a/statement.go b/statement.go index 453e485e..5a948d3f 100644 --- a/statement.go +++ b/statement.go @@ -173,7 +173,12 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) { case clause.Column, clause.Table: stmt.QuoteTo(writer, v) case Valuer: - stmt.AddVar(writer, v.GormValue(stmt.Context, stmt.DB)) + reflectValue := reflect.ValueOf(v) + if reflectValue.Kind() == reflect.Ptr && reflectValue.IsNil() { + stmt.AddVar(writer, nil) + } else { + stmt.AddVar(writer, v.GormValue(stmt.Context, stmt.DB)) + } case clause.Expr: v.Build(stmt) case *clause.Expr: