From 77bf4aecc6e5a156aff47b26a0dbb0dd4a31382a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 18 Dec 2020 13:25:52 +0800 Subject: [PATCH] Create associations w/o nested transaction option --- callbacks/associations.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/callbacks/associations.go b/callbacks/associations.go index 9e767e5e..f5c9e4be 100644 --- a/callbacks/associations.go +++ b/callbacks/associations.go @@ -296,7 +296,10 @@ func SaveAfterAssociations(db *gorm.DB) { } if joins.Len() > 0 { - db.AddError(db.Session(&gorm.Session{NewDB: true}).Clauses(clause.OnConflict{DoNothing: true}).Create(joins.Interface()).Error) + db.AddError(db.Session(&gorm.Session{NewDB: true}).Clauses(clause.OnConflict{DoNothing: true}).Session(&gorm.Session{ + SkipHooks: db.Statement.SkipHooks, + DisableNestedTransaction: true, + }).Create(joins.Interface()).Error) } } } @@ -355,7 +358,10 @@ func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, } } - tx := db.Session(&gorm.Session{NewDB: true}).Clauses(onConflict).Session(&gorm.Session{SkipHooks: db.Statement.SkipHooks}) + tx := db.Session(&gorm.Session{NewDB: true}).Clauses(onConflict).Session(&gorm.Session{ + SkipHooks: db.Statement.SkipHooks, + DisableNestedTransaction: true, + }) if len(selects) > 0 { tx = tx.Select(selects)