SQLC []byte array not getting inserted in postgres for jsonb column

95 views Asked by At

I'm new to sqlc and go. I have a jsonb column in postgres. When I do '''sqlc generate''', the types generated are []byte array exactly what the SQLC documentation says. But the problem I have is the geneated function which should be inserting the '''[]byte''' into postgress column is throwing error as Not a valid JSON type. From my little understanding the []byte has to be converted to jsonb again. But I guess I can't edit the geneated code as it will generate again if I regenerate.

Even if I make a custom struct to say SQLC to not generate jsonb as '''[]byte''' instead the custom type. Here also I assume I ll somehow need to convert that struct to jsonb which postgres will allow inserting.

Stuck on this. Is there any solution to it?

Slqc generation

-- name: CreateTeamUser :one
INSERT INTO team_users (
    meta_jsonb,

) VALUES (
  $1
) RETURNING *;

Slql genarated code after sqlc generate

type CreateTeamUserParams struct {
    Meta_jsonb   []byte      `json:"meta_jsonb"`
}

func (q *Queries) CreateTeamUser(ctx context.Context, arg CreateTeamUserParams) (TeamUser, error) {
    row := q.db.QueryRow(ctx, createTeamUser,
        arg.Meta_jsonb,
    )
    var i TeamUser
    err := row.Scan(
        &i.ID,
        &i.Meta_jsonb
    )
    return i, err
}

This is how I'm calling

arg := db.CreateTeamUserParams{
            Meta_jsonb:   user.GetMeta_Jsonb(),
        }

        _, err := server.store.CreateTeamUser(ctx, arg)

Data Im passing

{"meta_jsonb": {"name": "me"}}

Error Im getting

ERR received a HTTP request body="{\"code\":13,\"message\":\"failed to create user: ERROR: invalid input syntax for type json (SQLSTATE 22P02)\"}"
0

There are 0 answers