Missing Transaction For Multi Write (ARCH5065)
Esta página aún no está disponible en tu idioma.
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.
Incorrect
Section titled “Incorrect”db.Create(&order)db.Create(&payment) // if the first succeeds and this fails, inconsistent stateCorrect
Section titled “Correct”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.