diff --git a/gorm.go b/gorm.go index b1544d33..52cbd554 100644 --- a/gorm.go +++ b/gorm.go @@ -401,11 +401,11 @@ func Expr(expr string, args ...interface{}) clause.Expr { // ExprToString clause.Expression Build to SQL String func (db *DB) ExprToString(expr clause.Expression) string { stmt := &Statement{ - DB: db, + DB: db.Session(&Session{DryRun: true, SkipDefaultTransaction: true}), Clauses: map[string]clause.Clause{}, } expr.Build(stmt) - return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...) + return stmt.DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...) } // SetupJoinTable setup join table schema diff --git a/tests/sql_builder_test.go b/tests/sql_builder_test.go index e6c5f557..24356f18 100644 --- a/tests/sql_builder_test.go +++ b/tests/sql_builder_test.go @@ -365,7 +365,7 @@ func TestExprToString(t *testing.T) { wantSQL := "((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))" gotSQL := DB.ExprToString(clause.And(exprs...)) if wantSQL != gotSQL { - t.Fatalf("want: %s \n, got: %s", wantSQL, gotSQL) + t.Errorf("want: %s \n, got: %s", wantSQL, gotSQL) } sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { @@ -378,7 +378,7 @@ func TestExprToString(t *testing.T) { }) wantSQL = "SELECT * FROM `users` WHERE start_time >= 1234 AND (((`age` > 10 AND `age` < 18) AND (`age` > 18 AND `age` < 21))) AND `users`.`deleted_at` IS NULL" if wantSQL != sql { - t.Fatalf("want: %s \n, got: %s", wantSQL, sql) + t.Errorf("want: %s \n, got: %s", wantSQL, sql) } }