{"id":1247,"hash":"5890aed510e21c049ac6309067fe2084d0d4e3eef675d6552e442365a263c749","pattern":"unsupported data type: &amp;[] error on GORM field where custom Valuer returns nil?","full_message":"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).\n\ntype MultiString []string\n\nfunc (s *MultiString) Scan(src interface{}) error {\n    str, ok := src.(string)\n    if !ok {\n        return errors.New(\"failed to scan multistring field - source is not a string\")\n    }\n    *s = strings.Split(str, \",\")\n    return nil\n}\n\nfunc (s MultiString) Value() (driver.Value, error) {\n    if s == nil || len(s) == 0 {\n        return nil, nil\n    }\n    return strings.Join(s, \",\"), nil\n}\n\nThe problem arises when I try and call AutoMigrate on the following struct:\n\ntype Person struct {\n    ID      int\n    Name    string\n    Kids    *MultiString\n}\n\nI get the following errors multiple times:\n\n[error] unsupported data type: &[]","ecosystem":"go","package_name":"postgresql","package_version":null,"solution":"In the Value method, replace the returned nil with sql.NullString{} - This is incorrect, as Value should not return another Valuer.\n\nThe problem is that GORM is unsure of what the datatype should be for the newly defined type, so it tried to figure it out. Instead, the type should be explicitly defined, either with a tag in the model or by implementing a gorm method on the new type\n\nTag in the model\n\ntype MyModel struct {\n    ...\n    MyText MultiString `gorm:\"type:text\"`\n}\n\nThis tells GORM to use the type text for the DB column type. With this strategy, the tag must be applied every time the new type is used in a model.\n\nGORM Methods\n\nTwo GORM methods can be implemented on the new data type to tell GORM what database type should be used:\n\nGormDataType() string\nGormDBDataType(db *gorm.DB, field *schema.Field) string\n\nExamples:\n\nfunc (MultiString) GormDataType() string {\n  return \"text\"\n}\n\nfunc (MultiString) GormDBDataType(db *gorm.DB, field *schema.Field) string {\n\n  // returns different database type based on driver name\n  switch db.Dialector.Name() {\n  case \"mysql\", \"sqlite\":\n    return \"text\"\n  }\n  return \"\"\n}\n\nThis one is useful for if the data type differs between what type of database you're using.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/64035165/unsupported-data-type-error-on-gorm-field-where-custom-valuer-returns-nil","votes":17,"created_at":"2026-04-19T04:52:42.959412+00:00","updated_at":"2026-04-19T04:52:42.959412+00:00"}