Create associations w/o nested transaction option

This commit is contained in:
Jinzhu 2020-12-18 13:25:52 +08:00
parent 468152d45b
commit 77bf4aecc6
1 changed files with 8 additions and 2 deletions

View File

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