go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.9k stars 3.93k forks source link

Breaking change since v1.24.4: Custom serializer on nil-slices #6306

Open maja42 opened 1 year ago

maja42 commented 1 year ago

I already reported this at https://github.com/go-gorm/postgres/issues/159 a while ago, but it was ignored until now, probably because it's an issue in gorm itself. Since it's turning more and more into an issue for me, I decided to report it here as well:

go (simplified):

type ArgumentList []interface{}   // custom slice-type

// Never called, if `a` is nil (since v1.24.4):
func (a ArgumentList) Value() (driver.Value, error) {
    js, _ := a.MarshalJSON() // Returns a json-array. Nil-slices return "[]".
    return datatypes.JSON(js).Value()
}

func (a *ArgumentList) Scan(value interface{}) error {
    var js datatypes.JSON
    js.Scan(value)
    return json.Unmarshal(js, a)
}

Before 1.4.6, my serializers converted nil-slices to empty json-arrays ([] string). Now, they are ignored, nil gets converted into a PostgreSQL null, and my not-null-constraints are triggered. This prevents me from updating the gorm-dependency.

maja42 commented 1 year ago

The "workaround" for this (to support go get -u ./... in go modules) gets longer with each new gorm version:

in go.mod:

exclude gorm.io/gorm v1.24.4
exclude gorm.io/gorm v1.24.5
exclude gorm.io/gorm v1.24.6
exclude gorm.io/gorm v1.24.7
exclude gorm.io/gorm v1.25.0
exclude gorm.io/gorm v1.25.1

exclude gorm.io/driver/postgres v1.4.6
exclude gorm.io/driver/postgres v1.4.7
exclude gorm.io/driver/postgres v1.4.8
exclude gorm.io/driver/postgres v1.5.0
github-actions[bot] commented 1 year ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

black-06 commented 1 year ago

This is related to the postgres-driver, see https://github.com/jackc/pgx/issues/1566 for more infomation.

github-actions[bot] commented 6 months ago

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days