Skip to content

why json.RawMessage inserts but not []byte #2492

@PriyadarshanChougule

Description

@PriyadarshanChougule

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions