diff --git a/finisher_api.go b/finisher_api.go index 4a3c323b..2d7409c7 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -565,7 +565,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er func (db *DB) Begin(opts ...*sql.TxOptions) *DB { var ( // clone statement - tx = db.Session(&Session{Context: db.Statement.Context}) + tx = db.getInstance().Session(&Session{Context: db.Statement.Context}) opt *sql.TxOptions err error ) diff --git a/tests/transaction_test.go b/tests/transaction_test.go index c17fea3b..4e4b6149 100644 --- a/tests/transaction_test.go +++ b/tests/transaction_test.go @@ -41,7 +41,8 @@ func TestTransaction(t *testing.T) { t.Fatalf("Should not find record after rollback, but got %v", err) } - tx2 := DB.Begin() + txDB := DB.Where("fake_name = ?", "fake_name") + tx2 := txDB.Session(&gorm.Session{NewDB: true}).Begin() user2 := *GetUser("transaction-2", Config{}) if err := tx2.Save(&user2).Error; err != nil { t.Fatalf("No error should raise, but got %v", err)