Pgx: "wrong element type" when using JSONBArray

Created on 2 Sep 2020  路  1Comment  路  Source: jackc/pgx

I am trying to create a new row that has an inventory, which is a jsonb[]:

    elements := []pgtype.Text{{String: `{"adsda": "asdasd"}`, Status: pgtype.Present}}
    dimensions := []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}}
    inventory := pgtype.JSONBArray{Elements: elements, Dimensions: dimensions, Status: pgtype.Present}
    row = db.pool.QueryRow(context.Background(), `INSERT INTO user ("email", "password", "inventory") VALUES($1, $2, $3) RETURNING uuid, email, "password"`, requestEmail, requestPassword, inventory)

I am then getting the following error:

        "Severity": "ERROR",
        "Code": "42804",
        "Message": "wrong element type",
        "Detail": "",
        "Hint": "",
        "Position": 0,
        "InternalPosition": 0,
        "InternalQuery": "",
        "Where": "",
        "SchemaName": "",
        "TableName": "",
        "ColumnName": "",
        "DataTypeName": "",
        "ConstraintName": "",
        "File": "arrayfuncs.c",
        "Line": 1316,
        "Routine": "array_recv"

DDL:

-- public.user definition

-- Drop table

-- DROP TABLE public.user;

CREATE TABLE public.user (
    uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
    email varchar(64) NOT NULL,
    "password" varchar(32) NOT NULL,
    inventory _jsonb NULL,
    CONSTRAINT user_pk PRIMARY KEY (uuid)
);

What might be the issue?

Most helpful comment

It looks like there was a bug in the JSONBArray. It should never have had pgtype.Text elements. It should have had pgtype.JSONB elements. I don't think that ever worked... 馃槩

I've corrected this on master of the pgtype library. Try this: go get github.com/jackc/pgtype@79b0521. So I think it should work for you once you change the elements to the proper type. Let me know if that fixes it.

>All comments

It looks like there was a bug in the JSONBArray. It should never have had pgtype.Text elements. It should have had pgtype.JSONB elements. I don't think that ever worked... 馃槩

I've corrected this on master of the pgtype library. Try this: go get github.com/jackc/pgtype@79b0521. So I think it should work for you once you change the elements to the proper type. Let me know if that fixes it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nokel81 picture Nokel81  路  6Comments

bernhardreiter picture bernhardreiter  路  7Comments

Oliver-Fish picture Oliver-Fish  路  4Comments

skipcloud picture skipcloud  路  4Comments

pengux picture pengux  路  3Comments