2 回答

TA貢獻1883條經驗 獲得超3個贊
定義 JSON 類型。
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
)
type JSON json.RawMessage
func (j *JSON) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
result := json.RawMessage{}
err := json.Unmarshal(bytes, &result)
*j = JSON(result)
return err
}
func (j JSON) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return json.RawMessage(j).MarshalJSON()
}
在模型結構中:
type FlowTransaction struct {
gorm.Model
Name string
PartnerTransactionReferenceId string
ReconData JSON `type:jsonb`
DestinationAccountNumber *string
DestinationIfsc *string
Amount uint64
PartnerId uint
FlowId uint
SelfSettle bool
IsSandbox bool
}

TA貢獻1946條經驗 獲得超3個贊
因為 gorm 使用pgx,你可以使用pgtype包。有類型JSONB
所以你的模型看起來像這樣
import (
...
"github.com/jackc/pgtype"
...
)
type FlowTransaction struct {
gorm.Model
Name string
PartnerTransactionReferenceId string
ReconData pgtype.JSONB `gorm:"type:jsonb"`
DestinationAccountNumber *string
DestinationIfsc *string
Amount uint64
PartnerId uint
FlowId uint
SelfSettle bool
IsSandbox bool
}
- 2 回答
- 0 關注
- 438 瀏覽
添加回答
舉報