0

I need to save multiple tags in the single query:

valueStrings := make([]string, 0, len(tags))
valueArgs := make([]interface{}, 0, len(tags) * 3)
for _, tag := range tags {
    valueStrings = append(valueStrings, "(?, ?, ?)")
    valueArgs = append(valueArgs, tag)
    valueArgs = append(valueArgs, "NOW()")
    valueArgs = append(valueArgs, "NOW()")
}
stmt := fmt.Sprintf("INSERT INTO tags (name, created_at, updated_at) VALUES %s ON CONFLICT DO NOTHING;", strings.Join(valueStrings, ", "))
c.DB.Exec(stmt, valueArgs...)

Checking PostgreSQL logs:

ERROR:  syntax error at or near "," at character 58
STATEMENT:  INSERT INTO tags (name, created_at, updated_at) VALUES (?, ?, ?), (?, ?, ?) ON CONFLICT DO NOTHING;

I believe that Exec does not replace question marks with corresponding values.

Golang how do I batch sql statements with package database.sql

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.