jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
15.71k stars 1.07k forks source link

got an error when I execute tx.NamedQuery() #419

Open xiaoxfan opened 6 years ago

xiaoxfan commented 6 years ago

type Test struct { Id intjson:"id" db:"id" form:"id" Name stringjson:"name" db:"name" form:"name" Role pq.Int64Arrayjson:"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() 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() } the first err1 is nil ,but the others don't, here are output:

pq: unexpected Parse response 'D' driver: bad connection driver: bad connection driver: bad connection Process finished with exit code 0
xiaoxfan commented 6 years ago

anybody help! thank you.

xiaoxfan commented 6 years ago

sometimes I need to get the first sql's returning id as the other table's fk. So I can't use tx.NamedExec() function(which is work correctly).

denchenko commented 6 years ago

Same problem, but with 'C', not 'D'.

denchenko commented 6 years ago

@94Carousel I have figured it out. You just need to rows.Close() before executing next queries.