{"record":{"id":"a7391de1d1b93321","repo":"go-gorm/gorm","slug":"unsupported-data-type-table-not-set-please-set-i","errorCode":null,"errorMessage":"unsupported data type: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")","messagePattern":"unsupported data type: Table not set, please set it like: db\\.Model\\(&user\\) or db\\.Table\\(\"users\"\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"callbacks.go","lineNumber":113,"sourceCode":"\n\tif db.DefaultContextTimeout > 0 {\n\t\tif _, ok := stmt.Context.Deadline(); !ok {\n\t\t\tstmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)\n\t\t}\n\t}\n\n\t// assign model values\n\tif stmt.Model == nil {\n\t\tstmt.Model = stmt.Dest\n\t} else if stmt.Dest == nil {\n\t\tstmt.Dest = stmt.Model\n\t}\n\n\t// parse model values\n\tif stmt.Model != nil {\n\t\tif err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == \"\" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) {\n\t\t\tif errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == \"\" && stmt.TableExpr == nil {\n\t\t\t\tdb.AddError(fmt.Errorf(\"%w: Table not set, please set it like: db.Model(&user) or db.Table(\\\"users\\\")\", err))\n\t\t\t} else {\n\t\t\t\tdb.AddError(err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// assign stmt.ReflectValue\n\tif stmt.Dest != nil {\n\t\tstmt.ReflectValue = reflect.ValueOf(stmt.Dest)\n\t\tfor stmt.ReflectValue.Kind() == reflect.Ptr {\n\t\t\tif stmt.ReflectValue.IsNil() && stmt.ReflectValue.CanAddr() {\n\t\t\t\tstmt.ReflectValue.Set(reflect.New(stmt.ReflectValue.Type().Elem()))\n\t\t\t}\n\n\t\t\tstmt.ReflectValue = stmt.ReflectValue.Elem()\n\t\t}\n\t\tif !stmt.ReflectValue.IsValid() {\n\t\t\tdb.AddError(ErrInvalidValue)","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/callbacks.go#L95-L131","documentation":"In Execute's model parsing, when stmt.Parse(stmt.Model) fails with schema.ErrUnsupportedDataType AND the statement has no table (stmt.Table == \"\" and TableExpr == nil and SQL is empty), GORM wraps it as '%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")'. It tells you the Dest type cannot identify a table on its own and you did not supply one.","triggerScenarios":"Running a finisher where Dest is a non-struct scalar/container (e.g. db.Find(&ids) with ids []int, or db.Count(&n) without Model) and no db.Model(...) or db.Table(...) was chained, so parsing fails and there is no table to fall back on.","commonSituations":"Plucking into []int/[]string without Table/Model; db.Count without Model; Dest declared as map with no Table (map scanning requires Table); refactoring a method so the Model call was dropped; using raw struct-less queries after upgrading from GORM v1 behaviors.","solutions":["Chain db.Model(&User{}) (or db.Table(\"users\")) before the finisher.","For Pluck-style reads of one column, use db.Model(&User{}).Pluck(\"id\", &ids).","For map destinations, always set db.Table(\"...\") since maps have no schema.","Check the call chain in stack traces for a missing Model between Session and Find."],"exampleFix":"// before\nvar ids []int\ndb.Find(&ids) // unsupported data type: Table not set...\n\n// after\nvar ids []int\ndb.Model(&User{}).Pluck(\"id\", &ids)","handlingStrategy":"validation","validationCode":"// guard finishers: non-struct dest requires Model or Table\nfunc hasTableSource(tx *gorm.DB) bool {\n    return tx.Statement != nil && (tx.Statement.Table != \"\" || tx.Statement.TableExpr != nil || tx.Statement.Model != nil)\n}\n// in a wrapper:\nif !hasTableSource(db) && !isStructDest(dest) { return errors.New(\"set Model/Table\") }","typeGuard":"func isStructDest(dest interface{}) bool {\n    rv := reflect.Indirect(reflect.ValueOf(dest))\n    if rv.Kind() == reflect.Slice { rv = reflect.Indirect(rv.Type().Elem()) }\n    return rv.Kind() == reflect.Struct\n}","tryCatchPattern":"if err := db.Find(&ids).Error; err != nil {\n    if errors.Is(err, schema.ErrUnsupportedDataType) {\n        return errors.New(\"table not set: chain Model/Table before Find\")\n    }\n    return err\n}","preventionTips":["Always chain Model or Table before Find/Count/Pluck when Dest is not a model struct/slice.","Use Pluck for single-column reads instead of Find into []int/[]string.","Wrap finishers in small repository helpers that enforce the Model/Table contract."],"tags":["gorm","query","model-required","api-misuse","schema"],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}