go-gorm/gorm · error

unsupported data type

Error message

unsupported data type

What it means

schema.ErrUnsupportedDataType is returned by schema.Parse when a model cannot be turned into a Schema. The common causes: the model (or Dest of a query) is not a struct, is a nil/unsupported kind, or contains a field whose Go type has no usable serializer/data type (including fields whose underlying kind is invalid after following pointers).

Source

Thrown at schema/schema.go:33

	"gorm.io/gorm/logger"
)

type callbackType string

const (
	callbackTypeBeforeCreate callbackType = "BeforeCreate"
	callbackTypeBeforeUpdate callbackType = "BeforeUpdate"
	callbackTypeAfterCreate  callbackType = "AfterCreate"
	callbackTypeAfterUpdate  callbackType = "AfterUpdate"
	callbackTypeBeforeSave   callbackType = "BeforeSave"
	callbackTypeAfterSave    callbackType = "AfterSave"
	callbackTypeBeforeDelete callbackType = "BeforeDelete"
	callbackTypeAfterDelete  callbackType = "AfterDelete"
	callbackTypeAfterFind    callbackType = "AfterFind"
)

// ErrUnsupportedDataType unsupported data type
var ErrUnsupportedDataType = errors.New("unsupported data type")

type Schema struct {
	Name                      string
	ModelType                 reflect.Type
	Table                     string
	PrioritizedPrimaryField   *Field
	DBNames                   []string
	PrimaryFields             []*Field
	PrimaryFieldDBNames       []string
	Fields                    []*Field
	FieldsByName              map[string]*Field
	FieldsByBindName          map[string]*Field // embedded fields is 'Embed.Field'
	FieldsByDBName            map[string]*Field
	FieldsWithDefaultDBValue  []*Field // fields with default value assigned by database
	Relationships             Relationships
	CreateClauses             []clause.Interface
	QueryClauses              []clause.Interface
	UpdateClauses             []clause.Interface

View on GitHub (pinned to 1d6ce99528)

Solutions

  1. Ensure the model is a plain struct (or pointer to one) with an exported, defined type.
  2. Give custom field types Value()/Scan() methods (driver.Valuer / sql.Scanner) or a serializer gorm tag.
  3. For counts and scalars use proper destinations: var count int64; db.Model(&User{}).Count(&count).
  4. Inspect the exact field via the wrapped error context (set a logger to see the failing statement).

Example fix

// before
type Meta struct{ Data chan int } // chan has no data type

// after
type Meta struct{ Data string `gorm:"type:json"` }
Defensive patterns

Strategy: validation

Validate before calling

func validModel(v interface{}) error {
    rv := reflect.Indirect(reflect.ValueOf(v))
    if rv.Kind() != reflect.Struct {
        return fmt.Errorf("%T: %w", v, schema.ErrUnsupportedDataType)
    }
    for i := 0; i < rv.NumField(); i++ {
        ft := rv.Type().Field(i)
        if ft.Type.Kind() == reflect.Chan || ft.Type.Kind() == reflect.Func {
            return fmt.Errorf("field %s has unsupported type %s", ft.Name, ft.Type)
        }
    }
    return nil
}

Type guard

func isStructOrStructPointer(v interface{}) bool {
    return reflect.Indirect(reflect.ValueOf(v)).Kind() == reflect.Struct
}

Try / catch

if err := db.Find(&dest).Error; err != nil {
    if errors.Is(err, schema.ErrUnsupportedDataType) {
        return fmt.Errorf("bad model/dest type %T", dest)
    }
    return err
}

Prevention

When it happens

Trigger: Passing a non-struct as Model/Dest (db.Find(1), db.Find(&count)), declaring a field of an unsupported type with no Scanner/Valuer implementation and no serializer tag, or a model type that is an interface/untyped nil at runtime.

Common situations: Calling db.Model(&user).Count(&count) but passing count wrongly (e.g. &count where count is not int - that is a different error); fields of custom types lacking driver.Valuer/sql.Scanner; generics code where the type parameter does not constrain to struct; version upgrades tightening type validation.

Related errors


AI-assisted analysis of go-gorm/gorm@1d6ce99528 (2026-08-15). Data as JSON: /api/errors/f32c5785aa037246. Report an issue: GitHub.