jackc / pgtype

MIT License
308 stars 110 forks source link

ERROR: wrong element type (SQLSTATE 42804) #75

Closed stevesuh closed 3 years ago

stevesuh commented 3 years ago

I'm getting the following error trying to insert into a integer[] column in Postgres: ERROR: wrong element type (SQLSTATE 42804)

I'm hardcoding the value now just to see what's going on:

pgtype.Int2Array{
            Elements: []pgtype.Int2{
                {Int: 1, Status: pgtype.Present},
            },
            Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
            Status:     pgtype.Present,
        }

Any ideas?

stevesuh commented 3 years ago

I"m actually also interested in trying to get the equivalent of array[]::int[] in SQL via the Int2Array type.

jackc commented 3 years ago

The Go type pgtype.Int2Array is not the corresponding type for the PostgreSQL type int[]. You need to use pgtype.Int4Array.

stevesuh commented 3 years ago

Thank you that helped to get past the ERROR. I got the empty array I wanted as well. Thanks again.

pgtype.Int4Array{
            Elements: nil,
            Dimensions: nil,
            Status:     pgtype.Present,
        }