correct generated sql

This commit is contained in:
caelansar 2020-09-08 21:28:04 +08:00
parent c70c097e88
commit aceb3dad3b
2 changed files with 19 additions and 0 deletions

View File

@ -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(',')

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) {