diff --git a/statement.go b/statement.go index c6183724..0218064e 100644 --- a/statement.go +++ b/statement.go @@ -341,7 +341,9 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) [] if where, ok := cs.Expression.(clause.Where); ok { if len(where.Exprs) == 1 { if orConds, ok := where.Exprs[0].(clause.OrConditions); ok { - where.Exprs[0] = clause.AndConditions(orConds) + if len(orConds.Exprs) == 1 { + where.Exprs[0] = clause.AndConditions(orConds) + } } } conds = append(conds, clause.And(where.Exprs...)) diff --git a/tests/query_test.go b/tests/query_test.go index 566763c5..19cdeb59 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -632,6 +632,21 @@ func TestOr(t *testing.T) { t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String()) } + sub := dryDB.Clauses(clause.Where{ + Exprs: []clause.Expression{ + clause.OrConditions{ + Exprs: []clause.Expression{ + clause.Expr{SQL: "role = ?", Vars: []interface{}{"super_admin"}}, + clause.Expr{SQL: "role = ?", Vars: []interface{}{"admin"}}, + }, + }, + }, + }) + result = dryDB.Where(sub).Find(&User{}) + if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) { + t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String()) + } + result = dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{}) if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) { t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())