From 8901aadfede5e2e47d4eee8c2b415c0714602f4a Mon Sep 17 00:00:00 2001 From: Danny Date: Sat, 7 Feb 2026 00:04:50 +0700 Subject: [PATCH] Fix: JSONSlice stores "null" string instead of SQL NULL when nil Co-Authored-By: Claude Opus 4.6 --- json_type.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/json_type.go b/json_type.go index e7f5cc2..ccc7a86 100644 --- a/json_type.go +++ b/json_type.go @@ -103,6 +103,9 @@ func NewJSONSlice[T any](s []T) JSONSlice[T] { // Value return json value, implement driver.Valuer interface func (j JSONSlice[T]) Value() (driver.Value, error) { + if j == nil { + return nil, nil + } data, err := json.Marshal(j) if err != nil { return nil, err @@ -112,6 +115,10 @@ func (j JSONSlice[T]) Value() (driver.Value, error) { // Scan scan value into JSONType[T], implements sql.Scanner interface func (j *JSONSlice[T]) Scan(value interface{}) error { + if value == nil { + *j = nil + return nil + } var bytes []byte switch v := value.(type) { case []byte: @@ -143,6 +150,9 @@ func (JSONSlice[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string { } func (j JSONSlice[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr { + if j == nil { + return gorm.Expr("NULL") + } data, _ := json.Marshal(j) switch db.Dialector.Name() {