diff --git a/clause/where.go b/clause/where.go index 61aa73a87..20a011362 100644 --- a/clause/where.go +++ b/clause/where.go @@ -92,9 +92,14 @@ func (where Where) MergeClause(clause *Clause) { func And(exprs ...Expression) Expression { if len(exprs) == 0 { return nil - } else if len(exprs) == 1 { - return exprs[0] } + + if len(exprs) == 1 { + if _, ok := exprs[0].(OrConditions); !ok { + return exprs[0] + } + } + return AndConditions{Exprs: exprs} } diff --git a/soft_delete.go b/soft_delete.go index 51e4c0d7e..4582161dd 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -65,7 +65,7 @@ func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause) { func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) { if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok && !stmt.Statement.Unscoped { if c, ok := stmt.Clauses["WHERE"]; ok { - if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) > 1 { + if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) >= 1 { for _, expr := range where.Exprs { if orCond, ok := expr.(clause.OrConditions); ok && len(orCond.Exprs) == 1 { where.Exprs = []clause.Expression{clause.And(where.Exprs...)} diff --git a/tests/soft_delete_test.go b/tests/soft_delete_test.go index 0dfe24d5a..9ac8da10d 100644 --- a/tests/soft_delete_test.go +++ b/tests/soft_delete_test.go @@ -83,3 +83,13 @@ func TestDeletedAtUnMarshal(t *testing.T) { t.Errorf("Failed, result.DeletedAt: %v is not same as expected.DeletedAt: %v", result.DeletedAt, expected.DeletedAt) } } + +func TestDeletedAtOneOr(t *testing.T) { + actualSQL := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Or("id = ?", 1).Find(&User{}) + }) + + if !regexp.MustCompile(` WHERE id = 1 AND .users.\..deleted_at. IS NULL`).MatchString(actualSQL) { + t.Fatalf("invalid sql generated, got %v", actualSQL) + } +}