1.golang reported an error
2 when using sqlx"s transaction. The code is as follows
package main
import (
"log"
_ "github.com/lib/pq" // PostgreSQL
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"fmt"
)
type Test struct {
Id int `json:"id" db:"id" form:"id"`
Name string `json:"name" db:"name" form:"name"`
Role pq.Int64Array `json:"role" db:"role" form:"role"`
}
func main() {
db, err := sqlx.Connect("postgres", "user=postgres password=*** dbname=*** sslmode=disable")
if err != nil {
log.Panicln(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
tx, err := db.Beginx()
test := Test{Name: "name", Role: []int64{1, 2, 3}}
_, err1 := tx.NamedQuery(`INSERT INTO test(name,role) VALUES (:name,:role)returning id`, test)
fmt.Println(err1)
_, err1 = tx.NamedQuery(`INSERT INTO test(name,role) VALUES (:name,:role)returning id`, test)
fmt.Println(err1)
_, err1 = tx.NamedQuery(`INSERT INTO test(name,role) VALUES (:name,:role)returning id`, test)
fmt.Println(err1)
_, err1 = tx.NamedQuery(`INSERT INTO test(name,role) VALUES (:name,:role)returning id`, test)
fmt.Println(err1)
_, err1 = tx.NamedQuery(`INSERT INTO test(name,role) VALUES (:name,:role)returning id`, test)
fmt.Println(err1)
tx.Commit()
}
3. The output is as follows:
<nil>
pq: unexpected Parse response "D"
driver: bad connection
driver: bad connection
driver: bad connection
Process finished with exit code 0
4. It"s okay to replace namedquery () with namedexec (), but I want to get the primary key returned by the first statement before I can do the rest.
5. Ask the great god for an answer, thank you