go-gorm / go-gorm/postgres

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

Open
#159 3 comments 1 reaction 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
297
Forks
155
Avg merge
27m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.