Search code examples
gotransactionssqlx

Go: panic error after creating a new record with tx.MustExec


When I am using db.Exec it's working fine and creating record as well but it's giving panic. I don't know why. I am a beginner in golang so I am not sure what this error means and how to solve. My whole application works if I comment out panic part.

tx := db.MustBegin()
// _,err := db.Exec(queries.QueryInsertUserData, user.ID, user.Username, user.FirstName, user.LastName, user.Phone)
err := tx.MustExec("INSERT INTO users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) VALUES($1, now(), now(), NULL, $2, NULL, $3, $4, $5, true)", user.ID, user.Username, user.FirstName, user.LastName, user.Phone)
tx.Commit()

if err != nil {
  panic(err) // Giving error
}

Error:

2022/05/28 09:57:17 http: panic serving [::1]:53468: {0xc0001b6000 1}
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc0001b2280)
        /usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x13538e0, 0xc0000a8780)
        /usr/local/go/src/runtime/panic.go:975 +0x3e3
GO_APP/app/handler.CreateUser(0xc00009aea0, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /Users/kritisahu/Desktop/go_app/app/handler/users.go:61 +0x7ec
GO_APP/app.(*App).CreateUser(...)
        /Users/kritisahu/Desktop/go_app/app/app.go:47
github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc000182420, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /Users/kritisahu/go/pkg/mod/github.com/julienschmidt/[email protected]/router.go:387 +0xc37
net/http.serverHandler.ServeHTTP(0xc00018e0e0, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc0001b2280, 0x14000c0, 0xc0001ba040)
        /usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2933 +0x35c

Solution

  • You getting an error because Tx.MustExec does not return an error but it is returning an interface

    type Result interface {
        // LastInsertId returns the integer generated by the database
        // in response to a command. Typically this will be from an
        // "auto increment" column when inserting a new row. Not all
        // databases support this feature, and the syntax of such
        // statements varies.
        LastInsertId() (int64, error)
    
        // RowsAffected returns the number of rows affected by an
        // update, insert, or delete. Not every database or database
        // driver may support this.
        RowsAffected() (int64, error)
    }
    

    so in your code err stores Result interface value. instead of err write like this

    result := tx.MustExec("INSERT INTO users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) VALUES($1, now(), now(), NULL, $2, NULL, $3, $4, $5, true)", user.ID, user.Username, user.FirstName, user.LastName, user.Phone)
    
    fmt.Println(result.RowsAffected())