Fix new session with Begin, close #4120

This commit is contained in:
Jinzhu 2021-02-26 16:43:43 +08:00
parent ddeb143eb9
commit 189547f615
2 changed files with 3 additions and 2 deletions

View File

@ -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
)

View File

@ -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)