I was trying to create a GORM model for a postgres database, containing a type with a custom Scanner and Valuer that converts a string slice to and from a string, to be saved as a single database column. If the slice is either empty or nil, I want the database column to also be nil (as opposed to an empty string).
type MultiString []string
func (s *MultiString) Scan(src interface{}) error {
str, ok := src.(string)
if !ok {
return errors.New("failed to scan multistring field - source is not a string")
}
*s = strings.Split(str, ",")
return nil
}
func (s MultiString) Value() (driver.Value, error) {
if s == nil || len(s) == 0 {
return nil, nil
}
return strings.Join(s, ","), nil
}
The problem arises when I try and call AutoMigrate on the following struct:
type Person struct {
ID int
Name string
Kids *MultiString
}
I get the following errors multiple times:
[error] unsupported data type: &[]