I'm trying to refactor code so that I will connect to postgres DB using pgx, not pq. I've tried a lot of things, but I keep getting the ERROR: incorrect binary data format (SQLSTATE 22P03). Please find below both the original working code and my attempt at refactoring. What am I doing wrong?
// pq code
stmt, _ := txn.Prepare(pq.CopyIn(table, "id", "client_id", "channel_id", "feature"))
for _, user := range users{
_, err = stmt.Exec(stringA, stringB, integer64, pq.Array(stringArray))
}
//pgx code
rows := make([][]interface{}, len(users))
ix := 0
for _, user := range users{
rows[ix] = []interface{}{stringA, stringB, integer64, stringArray}
ix++
}
_, err = txn.CopyFrom(context.Background(), pgx.Identifier{table},
[]string{"id", "client_id", "channel_id", "feature"}, pgx.CopyFromRows(rows))
EDIT: I got a hint from the pgx documentation: CopyFrom requires all values use the binary format.
The problem was my id (noted as stringA in the code above). It was created as:
stringA := uuid.New().String()
I augmented it with pgtype and now it works like a charm:
newStringA := &pgtype.UUID{}
_ := idPgtype.Set(stringA)