jschaf / pggen

Generate type-safe Go for any Postgres query. If Postgres can run the query, pggen can generate code for it.
MIT License
281 stars 26 forks source link

Error when using bytea datatype #82

Open Oliver-Fish opened 1 year ago

Oliver-Fish commented 1 year ago

Before this commit https://github.com/jschaf/pggen/commit/2489d54a806652d73f2c83bb5736a7574582092c I was able to do the following.

CREATE TABLE table_name (
    id BIGSERIAL PRIMARY KEY,
    data BYTEA
);
-- name: InsertData :exec
INSERT INTO table_name (data)
    VALUES (pggen.arg ('data'));

However, from that commit onwards, I now get the following error.

ERROR: generate go code: template all: template query file /path/file.sql for go: resolve known type &{"" %!q(*gotype.OpaqueType=&{<nil> byte})} does not have pg array type "bytea"

My command looks like the following;

pggen gen go \
    --output-dir ./../postgres \
    --query-glob './queries/*.sql' \
    --postgres-connection "user=postgres port=5432 dbname=databasename password=password host=localhost"

It appears this changed the behavior https://github.com/jschaf/pggen/commit/2489d54a806652d73f2c83bb5736a7574582092c#diff-c42aa8077d6b0565a2ffc41636d61300a263997cbe44e90c16b7db051f9f9fdeR50

According to the comments on pg.ArrayType

// ArrayType is an array type where pg_type.typelem != 0 and the name begins
// with an underscore.

However, this is not the case for bytea.

Bytea            = BaseType{ID: pgtype.ByteaOID, Name: "bytea"}

I was able to overcome this error by using the following command instead. However, this feels like it should be the default behavior.

pggen gen go \
    --output-dir ./../postgres \
    --query-glob './queries/*.sql' \
    --go-type bytea=[]byte \
    --postgres-connection "user=postgres port=5432 dbname=databasename password=password host=localhost"
jschaf commented 1 year ago

Thank you, definitely looks like a regression.