Let's say I have a micro-service in golang
using pgx
to connect to postgres
database.
I have a data structure that has a enum type:
CREATE TYPE "direction_type" AS ENUM (
'LEFT',
'RIGHT',
'UP',
'DOWN'
);
CREATE TABLE "move_log" (
ID uuid PRIMARY KEY,
direction direction_type,
steps int4
)
So when I try to insert a record with:
insertMove := "INSERT INTO move_log (id, direction, steps) values ($1, $2, $3)"
_, err := d.db.ExecContext(ctx, insertMove, uid, "LEFT", steps)
it fails with
2 UNKNOWN: ERROR: invalid input value for enum direction_type: "LEFT" (SQLSTATE 22P02)
I've found a pgx type enum_array, but I have no idea on how to use it.
I found this answer: https://github.com/jackc/pgx/issues/338#issuecomment-333399112
insertMove := "INSERT INTO move_log (id, direction, steps) values ($1, $2::text[]::direction_type[], $3)"
Works.