jackc / jackc/pgx

why json.RawMessage inserts but not []byte

Open
#2,492 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

when i try to insert struct directly by marshalling like this (payload is marshalled and passed to this function)

```
import "github.com/jackc/pgx/v5"

func InsertDummyRecord(ctx context.Context, tx pgx.Tx, accountId interface{}, action, state string, payload []byte, createdBy, updatedBy int64, groupId *int64) (int64, error) {
query := `INSERT INTO dummy_table (
account_id,
action,
state,
payload,
created_by,
updated_by,
group_id
) VALUES ($1,$2,$3,$4,$5,$6,$7)
RETURNING id`

var recordId int64

err := tx.QueryRow(ctx, query, accountId, action, state,payload, createdBy, updatedBy, groupId).Scan(&recordId)
if err != nil {
return 0, err
}
return recordId, nil
}
```

this fails and i get this error
Severity ="ERROR"
SeverityUnlocalized ="ERROR"
Code ="22P02"
Message ="invalid input syntax for type json"

but if i do
) VALUES ($1,$2,$3,$4::jsonb,$5,$6,$7)
and
err := tx.QueryRow(ctx, query, accountId, action, state, string(payload), createdBy, updatedBy, groupId).Scan(&recordId)

it works

also if type in go is json.RawMessage the above function works directly without explicit ::jsonb and string(). Since []byte and json.RawMessage are internally same , why does []byte fail and json.RawMessage works ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the InsertDummyRecord example and the pgx.QueryRow call, then reproduce the difference between []byte and json.RawMessage against the JSONB column. Compare how each argument type is encoded and check the surrounding pgx documentation or tests; done means the behavior and expected workaround are clearly explained, or a focused regression is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.