From 189547f615919db93a70a7c48ffe4ad819d14962 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 26 Feb 2021 16:43:43 +0800 Subject: [PATCH] Fix new session with Begin, close #4120 --- finisher_api.go | 2 +- tests/transaction_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) 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)