correct generated sql

This commit is contained in:
caelansar 2020-09-08 21:28:04 +08:00 committed by Jinzhu
parent 222427c474
commit 839e09e985
2 changed files with 19 additions and 0 deletions

View File

@ -37,6 +37,9 @@ func (expr Expr) Build(builder Builder) {
} else { } else {
switch rv := reflect.ValueOf(expr.Vars[idx]); rv.Kind() { switch rv := reflect.ValueOf(expr.Vars[idx]); rv.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if rv.Len() == 0 {
builder.AddVar(builder, nil)
}
for i := 0; i < rv.Len(); i++ { for i := 0; i < rv.Len(); i++ {
if i > 0 { if i > 0 {
builder.WriteByte(',') builder.WriteByte(',')

View File

@ -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) { func TestQueryWithAssociation(t *testing.T) {