go-gorm / postgres

GORM PostgreSQL driver
MIT License
228 stars 120 forks source link

Breaking change since 1.4.6: nil-Slices with custom driver.Valuer / sql.Scanner #159

Open maja42 opened 1 year ago

maja42 commented 1 year ago

This is a follow-up to #152, because the proposed "solution" / workaround doesn't work. The ticket was already closed though.

In PostgreSQL, the column has type JSONB.

go (simplified):

type ArgumentList []interface{}

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)
}

func (ArgumentList) GormDataType() string {
    return datatypes.JSON{}.GormDataType()
}

func (ArgumentList) GormDBDataType(db *gorm.DB, field *schema.Field) string {
    return datatypes.JSON{}.GormDBDataType(db, field)
}

I'm already using datatypes.JSON in my custom serializer / scanner, as proposed by the other ticket. However, they are never called for nil-slices.

Before 1.4.6, my serializers converted nil-slices to empty json-arrays. Now, they are ignored, nil gets converted into a PostgreSQL null, and my not-null-constraints are triggered. So this is definitely a breaking change.

maja42 commented 1 year ago

Temporary workaround for go.mod:

exclude gorm.io/gorm v1.24.4
exclude gorm.io/gorm v1.24.5
exclude gorm.io/driver/postgres v1.4.6
exclude gorm.io/driver/postgres v1.4.7

Maybe this is (also) a bug in gorm.io/gorm?

maja42 commented 1 year ago

Updated exclude list:

exclude (
    gorm.io/driver/postgres v1.4.6
    gorm.io/driver/postgres v1.4.7
    gorm.io/driver/postgres v1.4.8
    gorm.io/driver/postgres v1.5.0
    gorm.io/driver/postgres v1.5.1
    gorm.io/driver/postgres v1.5.2
    gorm.io/driver/postgres v1.5.3

    gorm.io/gorm v1.24.4
    gorm.io/gorm v1.24.5
    gorm.io/gorm v1.24.6
    gorm.io/gorm v1.25.0
    gorm.io/gorm v1.25.1
    gorm.io/gorm v1.25.2
)
maja42 commented 10 months ago

The list is getting longer:

exclude (
    gorm.io/driver/postgres v1.4.6
    gorm.io/driver/postgres v1.4.7
    gorm.io/driver/postgres v1.4.8
    gorm.io/driver/postgres v1.5.0
    gorm.io/driver/postgres v1.5.1
    gorm.io/driver/postgres v1.5.2
    gorm.io/driver/postgres v1.5.3
    gorm.io/driver/postgres v1.5.4

    gorm.io/gorm v1.24.4
    gorm.io/gorm v1.24.5
    gorm.io/gorm v1.24.6
    gorm.io/gorm v1.25.0
    gorm.io/gorm v1.25.1
    gorm.io/gorm v1.25.2
    gorm.io/gorm v1.25.3
    gorm.io/gorm v1.25.4
    gorm.io/gorm v1.25.5
)