From 8c9667cc376ec1c2c1f47d5c9f1a22cdbc64a1b7 Mon Sep 17 00:00:00 2001 From: Gilad Weiss Date: Thu, 20 Jan 2022 19:04:34 +0200 Subject: [PATCH] Inherit clone flag (NewDB) on transaction creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- finisher_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/finisher_api.go b/finisher_api.go index 355d89bd..757a9333 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -585,7 +585,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 )