rocketlaunchr / dbq

Zero boilerplate database operations for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Postgres/pq bulk insert syntax issue

KeithAlt opened this issue · comments

commented

Dependencies:

	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
	"github.com/rocketlaunchr/dbq/v2"
	"net/http"

Code of issue:

	psqlInfo := fmt.Sprintf("%s://%s:%s@%s:%s/%s?sslmode=%s",
		"postgres", "postgres", "postgres", "localhost", "5432", "", "disable",
	)

	db, _ := sql.Open("postgres", psqlInfo)

	type Row struct {
		Username string
		Password string
	}

	users := []interface{}{
		dbq.Struct(Row{"John", "abc123"}),
		dbq.Struct(Row{"Mark", "abc123"}),
		dbq.Struct(Row{"David", "abc123"}),
	}

	stmt := dbq.INSERTStmt("accounts", []string{"username", "password"}, len(users))
	_, err := dbq.E(ctx, db, stmt, nil, users) // <- always results in syntax error
	if err != nil {
		panic(err) // <- pq: syntax error at or near ","
		return
	}

Output: pq: syntax error at or near ","

Consistently results in a syntax error.
If the cause is the above code, let me know 👍

commented

You need to set option to postgres mode

commented

You need to set option to postgres mode

My mistake.
Thanks! 👍

commented

Also use a faster postgres driver instead of pq