go-gorm / postgres

GORM PostgreSQL driver
MIT License
234 stars 123 forks source link

1.4.6 is a breaking change! "W10=" instead of "[]" #152

Closed yvz5 closed 1 year ago

yvz5 commented 1 year ago

Don't have time to create a playground link. its just too much work. updating 1.4.5 to 1.4.6 broke our system.

Description

we have a table with a column of type jsonb in postgres. in go, we represent this column with json.RawMessage . in version 1.4.5 we can save an empty array into jsonb column and it would save [] in to the table. now, in the new version (1.4.6), it saves the value "W10=" instead of [] which can not be unmarshalled to a known object in go.

maja42 commented 1 year ago

I implemented the driver.Valuer interface on a custom slice-type. It produces [] for both nil-slices and empty slices (go best-practices state that they should be handled equivalently, so I serialize both variants to the same database value). Since 1.4.6, nil-slices are no longer converted to [], but to null.

This took me a while to figure out. Luckily I had unit-tests that triggered not-null-constraint violations in my database.

(Workaround: add exclude gorm.io/driver/postgres v1.4.6 to the go.mod file)

numaga94 commented 1 year ago

I had the same issue when I upgraded from v1.4.5 to v1.4.6, however, I figure it out that the issue was caused by customised JSON type in GORM 1.0. Since I changed the customised JSON type to official GORM DataTypes, the problem went away.

Please refer the mention on the release note of GORM 2.0 at https://gorm.io/docs/v2_release_note.html#DataTypes-JSON-as-example.

yvz5 commented 1 year ago

@numaga94 thanks for your hint. it actually solved the problem. Use datatypes.JSON

maja42 commented 1 year ago

See also: https://github.com/jackc/pgx/issues/1566