From f3547e00cc786e0b07206c775f3b7fe19164f56f Mon Sep 17 00:00:00 2001 From: Gilad Weiss Date: Sun, 20 Feb 2022 02:33:12 +0200 Subject: [PATCH] Inherit clone flag (NewDB) on transaction creation (#5012) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Inherit clone flag (NewDB) on transaction creation I find it very reassuring to know that after a finisher API, I get a clean db object for my next queries. If you look at the example in https://gorm.io/docs i’d see many queries running one after the other.. but in reality they wouldn’t work as the they are portrayed and that’s because in default mode NewDB is false and will make all the clauses stay even after a finisher API. My solution is just to have the value of the clone flag in the “parent” db object, be injected to its children transactions. * Fix typo --- finisher_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/finisher_api.go b/finisher_api.go index d2a8b981..f994ec31 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -590,7 +590,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.getInstance().Session(&Session{Context: db.Statement.Context}) + tx = db.getInstance().Session(&Session{Context: db.Statement.Context, NewDB: db.clone == 1}) opt *sql.TxOptions err error )