Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to form bulk upsert query #722

Open
lenonqing opened this issue Apr 9, 2021 · 8 comments
Open

unable to form bulk upsert query #722

lenonqing opened this issue Apr 9, 2021 · 8 comments

Comments

@lenonqing
Copy link

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.

@gurza
Copy link

gurza commented May 7, 2021

@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)

@colinrgodsey
Copy link

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.

@dan-r95
Copy link

dan-r95 commented Nov 30, 2022

Any update on this?

@QuangTung97
Copy link
Contributor

QuangTung97 commented Nov 30, 2022

Any update on this?

I think version >= 1.3.4 should already fixed this

@lenonqing
Copy link
Author

yes, version >= 1.3.4 is ok.

@Odas0R
Copy link

Odas0R commented May 18, 2023

I can confirm, this works on v1.3.5!

Maybe close this issue?

@SenselessA
Copy link

SenselessA commented Jul 12, 2023

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

@yzinkovets
Copy link

same problem on 1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants