go-gorm/datatypes

Getting issues while using JSONSlice for Postgres []

Opened this issue · 3 comments

Your Question

How can I use JSONSlice for JSON[] fields with gorm, I have used the library but getting an error while inserting the data. The Table model struct is attached below.

The document you expected this should be explained

https://go.dev/play/p/aEEKW7fUa72

I am getting issue when the arrays are empty in the database layer.

UTC [268] ERROR: malformed array literal: "null"
postgres-1 | 2024-08-06 15:18:34.167 UTC [268] DETAIL: Array value must start with "{" or dimension information.

Expected answer

postgres array type format is like '{ val1 delim val2 delim ... }', It's not a JSON style data.

see https://www.postgresql.org/docs/current/arrays.html

postgres array type format is like '{ val1 delim val2 delim ... }', It's not a JSON style data.

see https://www.postgresql.org/docs/current/arrays.html

I m not passing any data, that should be valid right, if you see my message carefully, i have mentioned that I passing empty field, shouldn't the library handle that?

@venky-connectwise the JSONSlice now is for JSON type column , It is decode the field value as a slice, not the pg's JSON[] filed type. it's first JSON and then use as slice, it can be

json <-> []string
json <-> []struct
json <-> []int

the value should be json style

["1a", "2b", "3c"]

what you want is

JSON[] <-> []any

the pg's array style is like

{"1a", "2b", "3c"}

it can't decode by json.Unmarshal

Maybe we can support the pg's Array by add another generic type ?