sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

unable to form bulk upsert query

Open lenonqing opened this issue 4 years ago • 8 comments

in v 1.3.3

query := "INSERT INTO TABLE_VALUES (`tid`, `uid`, `count`, `deposit`, `ver`) VALUES (:tid, :uid, :count, :deposit, :ver) ON DUPLICATE KEY UPDATE `tid`=VALUES(`tid`), `uid`=VALUES(`uid`), `count`=VALUES(`count`), `deposit`=VALUES(`deposit`), `ver`=VALUES(`ver`)"
args := []map[string]interface{}{
        {"tid": 1, "uid": 1, "count": 1, "deposit": 0, "ver": 0},
        {"tid": 2, "uid": 1, "count": 1, "deposit": 0, "ver": 0},
    }
query, argsOut, err := sqlx.Named(query, args)
_, err = db.ExecContext(ctx, query, argsOut...)

got sql: expected 5 arguments, got 10 error.

lenonqing avatar Apr 09 '21 08:04 lenonqing

@lenonqing you can call bulk upset with []map[string]interface{} directly. It works if you need quick solution.

q := "INSERT INTO role(type,value) VALUES (:type, :value)"

pp := []map[string]interface{}{
	{"type": "type1", "value": "val1"},
	{"type": "type2", "value": "val2"},
	{"type": "type3", "value": "val3"},
}

res, err := db.NamedExec(q, pp)

gurza avatar May 07 '21 13:05 gurza

it'd be nice to fix this. if you put anything after the VALUES(...) segment, the bulk insert seems to fail. This makes it impossible to use any ON CONFLICT ... forms of the insert.

colinrgodsey avatar May 07 '21 18:05 colinrgodsey

Any update on this?

dan-r95 avatar Nov 30 '22 09:11 dan-r95

Any update on this?

I think version >= 1.3.4 should already fixed this

QuangTung97 avatar Nov 30 '22 10:11 QuangTung97

yes, version >= 1.3.4 is ok.

lenonqing avatar Dec 01 '22 03:12 lenonqing

I can confirm, this works on v1.3.5!

Maybe close this issue?

Odas0R avatar May 18 '23 10:05 Odas0R

I got the same problem with 1.3.5 version. Error: mismatched param and argument count

SenselessA avatar Jul 12 '23 16:07 SenselessA

same problem on 1.4.0

yzinkovets avatar Jul 18 '24 09:07 yzinkovets