From what I read and understood from the docs that Gorm support nested transaction rollback, which means that if any of the transaction fail, it should rollback everything / cancel all commits right? However it is not working as how I understood it.
order.go
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// Create some data in orders table
if err := tx.Model(&models.Order{}).Create(map[string]any{"user_id": 121345}).Error; err != nil {
tx.Rollback()
return err
}
// Create some data in order_payments table
if err := tx.Model(&models.OrderPayment{}).Create(map[string]any{"amount": "zzsadq"}).Error; err != nil {
tx.Rollback()
// When this fails, it does not rollback the data in orders table, there are still new data
// being created in the orders table, as I understood, it should rollback all the
// transactions
return err
}
return tx.Commit().Error
Is there something missing, or am I not understanding correctly? If so, how can I rollback all the previous transactions if any of them fails? I need to rollback everything for data consistency. Thanks a lot in advance