mysql - Golang gorm how to use `where in` with slice of ints -


from http://jinzhu.me/gorm/advanced.html#sql-builder, should able update multiple rows using in single (?) , passing slice single ? opposed where in (?,?,?,?).

example jinzhu.me follows: db.exec("update orders set shipped_at=? id in (?)", time.now, []int64{11,22,33}). here example of gorm's tests showing working. https://github.com/jinzhu/gorm/blob/021d7b33143de37b743d1cf660974e9c8d3f80ea/main_test.go#l449

this doesnt work me however:

var availableids []int _, p := range v.products {     availableids = append(availableids, p.id) }  log.println(availableids)  db.exec("update product_supplier set deleted_at=? supplier_id = ? , sku not in (?)", time.now(), 3, availableids) 

output:

2016/04/19 07:48:44 [336 338 337 306 329 94 79 43 57 313 108 122 126 127 124 125 123 221 93 330 335 333 312]  (sql: expected 2 arguments, got 25) 

when try hardcoding, same issue:

db.exec("update product_supplier set deleted_at=? supplier_id = ? , sku not in (?)", time.now(), 3, []int{313, 108}) 

output:

(sql: expected 2 arguments, got 4) 

solution:

the code not bugged @ all. me being silly - had parameter in actual code should have. didn't translate stack overflow correctly. bad.

the natural of prepare functionality prevents passing slice argument.

the implementation of db.exec in go first prepare query (including variable placeholders) , send arguments.

if wonder why prepare prevents passing slice, read this answer.

as workaround, same amount of placeholders size of slice need concatenated in program, should generating query this:

... supplier_id = ? , sku not in (?, ?, ?, ?) 

example code:

ids := []int{1, 2, 3} query := "... supplier_id = ? , sku not in (" + genvar(len(ids)) + ")" db.exec(query, 3, ids) 

update:

it turns gorm's implementation of db.exec method not using prepare functionality dbms, concatenate strings in driver.

my diagnosis there might wrong in dependencies.

are importing gorm shows in http://jinzhu.me/gorm/ ?


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -