jmoiron / sqlx

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

NamedExec doesn't support slice of struct? #519

Open davidxiao opened 5 years ago

davidxiao commented 5 years ago

Hi, I am using postgresql, when run the below

`

type person struct { FirstName string LastName string Gender string } persons := []person{ { "f1", "l1", "m", }, { "f2", "l2", "m", }, } `

insertCmd := INSERT INTO person(first_name, last_name, gender) VALUES(:firstname, :lastname, :gender) db.NamedExec(insertCmd, persons ) getting errors: panic: reflect: call of github.com/jmoiron/sql/reflectx.(*Mapper).TraversalsByNameFunc on slice Value,

I found tests https://github.com/jmoiron/sqlx/blob/master/named_test.go#L194 works, any advice?

solarhell commented 5 years ago

check if you are using the master brach.

nesymno commented 5 years ago

I'm using go dep -> dep ensure, use the master branch but in the vendor not the latest version of code...

waichee commented 4 years ago

hi will there be a new release soon to include the bug fixes?

gonejack commented 4 years ago

Code below would not work.

persons := []map[string]interface{}{
    {
        "first": "Bin",
        "last":  "Smuth",
        "email": "bensmith@allblacks.nz",
    },
    {
        "first": "Bin2",
        "last":  "Smuth2",
        "email": "bensmith@allblacks.nz2",
    },
}
_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, persons)

print(err)
cbndr commented 4 years ago

I noticed go get -u does not download the latest version (1.2.1) but an older one (1.2.0) if no entry in go.mod exists. That older version does not, for instance, support NamedExec with slices. I have to change the version manually to v1.2.1-0.20200324155115-ee514944af4b.

edisonqkj commented 3 years ago

I noticed go get -u does not download the latest version (1.2.1) but an older one (1.2.0) if no entry in go.mod exists. That older version does not, for instance, support NamedExec with slices. I have to change the version manually to v1.2.1-0.20200324155115-ee514944af4b.

it works. thanks