Transactions with invoices and orders

Report a typo

Below are code pieces of a Go program that uses GORM to perform operations within a transaction on the store.db SQLite database that has the following schema:

Your task is to arrange the pieces of code in the correct order to first create an Invoice record within the transaction and then create an Order record tied to an InvoiceID.

Put the items in the correct order
func main() {
  db, err := gorm.Open(sqlite.Open("store.db"), &gorm.Config{})
  if err != nil {
    log.Fatalf("cannot open store.db: %v", err)
  }
  tx := db.Begin()
  result = tx.Create(&order)
  if result.Error != nil {
    log.Printf("cannot create Order: %v", result.Error)
    tx.Rollback()
    return
  }
  tx.Commit()
}
  result := tx.Create(&invoice)
  if result.Error != nil {
    log.Printf("cannot create Invoice: %v", result.Error)
    tx.Rollback()
    return
  }
  order := Order{ProductName: "Hockey Mask", InvoiceID: invoice.ID}
  invoice := Invoice{ShippingAddress: "Camp Crystal Lake", TotalCost: 9.99}
___

Create a free account to access the full topic