Zum Inhalt springen

Missing Transaction For Multi Write (ARCH5065)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.orm.missing-transaction-for-multi-write · Severity warning · Category orm

Two or more dependent write operations in the same method that are not wrapped in a transaction risk partial commits. Use an atomic block.

db.Create(&order)
db.Create(&payment) // if the first succeeds and this fails, inconsistent state
db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&order).Error; err != nil {
return err
}
return tx.Create(&payment).Error
})

Wrap dependent multi-write operations in a single atomic block with db.Transaction.