Open buildpeak opened 4 years ago
This is an edge case with the simple protocol and database/sql
.
One problem is that when using the simple protocol is the destination PostgreSQL type is unknown. The type has to be determined by completely by the Go side. What is a []byte
encoded as? pgx considers it bytea
which means it is hex encoded. Use a string
for raw JSON data instead of []byte
.
The pgtype.JSON
case is more complicated. (As an aside, use pgtype.JSONB
for PostgreSQL jsonb
.) When using the pgx native interface there is a special case to handle pgtype.JSON
.
https://github.com/jackc/pgx/blob/eeda0368e66fafed0a3db500108bdb87b657a88a/values.go#L40
However, when using database/sql
, pgx can't see that it is a pgtype.JSON
, database/sql
has already called Value()
and converted it to a []byte
. And Value()
returns []byte
for compatibility with lib/pq
and json.RawMessage
(https://github.com/jackc/pgx/issues/409).
So I'm not sure if this exact case can be solved without breaking something else. But you can use string
, sql.NullString
, or pgtype.Text
instead.
@jackc do you know if this issue is impacted / resolved with the removal of the JSONB type? https://github.com/jackc/pgx/blob/master/CHANGELOG.md#other-changes
@aethanol It's still an issue with v5. Same fundamental problem though the internal implementation details differ.
One problem is that when using the simple protocol is the destination PostgreSQL type is unknown. The type has to be determined by completely by the Go side. What is a []byte encoded as? pgx considers it bytea which means it is hex encoded. Use a string for raw JSON data instead of []byte.
So I'm not sure if this exact case can be solved without breaking something else. But you can use
string
,sql.NullString
, orpgtype.Text
instead.
Thanks for the explanation @jackc.
I solved my issue by doing something like this:
-- schema.sql
CREATE TABLE IF NOT EXISTS my_table(
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
my_data jsonb
);
-- query.sql
-- name: InsertMyTable :exec
INSERT INTO my_table (data)
VALUES ((@my_data::text)::jsonb);
Please let me know if I missed something.
Error message
Description
When use pgx/v4/stdlib and sqlx to store pgtype.JSON to database, will get an error like above(SQLSTATE 22P02). I also tried database/sql and got the same errors. However, with
pgx.ConnectConfig(ctx, connConfig)
, pgtype.JSON worked but []byte not.Code to reproduce