diff --git a/clause/expression.go b/clause/expression.go index 3b914e68..55599571 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -37,6 +37,9 @@ func (expr Expr) Build(builder Builder) { } else { switch rv := reflect.ValueOf(expr.Vars[idx]); rv.Kind() { case reflect.Slice, reflect.Array: + if rv.Len() == 0 { + builder.AddVar(builder, nil) + } for i := 0; i < rv.Len(); i++ { if i > 0 { builder.WriteByte(',') diff --git a/tests/query_test.go b/tests/query_test.go index 795186da..e695e825 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -202,6 +202,22 @@ func TestFind(t *testing.T) { } } }) + + var models []User + if err := DB.Where("name in ?", []string{"find"}).Find(&models).Error; err != nil || len(models) != 3 { + t.Errorf("errors happened when query find with in clause: %v, length: %v", err, len(models)) + } else { + for idx, user := range users { + t.Run("FindWithInClause#"+strconv.Itoa(idx+1), func(t *testing.T) { + CheckUser(t, models[idx], user) + }) + } + } + + var none []User + if err := DB.Where("name in ?", []string{}).Find(&none).Error; err != nil || len(none) != 0 { + t.Errorf("errors happened when query find with in clause and zero length parameter: %v, length: %v", err, len(none)) + } } func TestQueryWithAssociation(t *testing.T) {