{"record":{"id":"307083096d333835","repo":"go-gorm/gorm","slug":"invalid-polymorphic-type-v-for-v-on-field-s-mi","errorCode":null,"errorMessage":"invalid polymorphic type %v for %v on field %s, missing field %s","messagePattern":"invalid polymorphic type (.+?) for (.+?) on field (.+?), missing field (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"schema/relationship.go","lineNumber":223,"sourceCode":"\t)\n\n\tif value, ok := field.TagSettings[\"POLYMORPHICTYPE\"]; ok {\n\t\ttypeName = strings.TrimSpace(value)\n\t}\n\n\tif value, ok := field.TagSettings[\"POLYMORPHICID\"]; ok {\n\t\ttypeId = strings.TrimSpace(value)\n\t}\n\n\trelation.Polymorphic.PolymorphicType = relation.FieldSchema.FieldsByName[typeName]\n\trelation.Polymorphic.PolymorphicID = relation.FieldSchema.FieldsByName[typeId]\n\n\tif value, ok := field.TagSettings[\"POLYMORPHICVALUE\"]; ok {\n\t\trelation.Polymorphic.Value = strings.TrimSpace(value)\n\t}\n\n\tif relation.Polymorphic.PolymorphicType == nil {\n\t\tschema.err = fmt.Errorf(\"invalid polymorphic type %v for %v on field %s, missing field %s\",\n\t\t\trelation.FieldSchema, schema, field.Name, polymorphic+\"Type\")\n\t}\n\n\tif relation.Polymorphic.PolymorphicID == nil {\n\t\tschema.err = fmt.Errorf(\"invalid polymorphic type %v for %v on field %s, missing field %s\",\n\t\t\trelation.FieldSchema, schema, field.Name, polymorphic+\"ID\")\n\t}\n\n\tif schema.err == nil {\n\t\trelation.References = append(relation.References, &Reference{\n\t\t\tPrimaryValue: relation.Polymorphic.Value,\n\t\t\tForeignKey:   relation.Polymorphic.PolymorphicType,\n\t\t})\n\n\t\tprimaryKeyField := schema.PrioritizedPrimaryField\n\t\tif len(relation.foreignKeys) > 0 {\n\t\t\tif primaryKeyField = schema.LookUpField(relation.foreignKeys[0]); primaryKeyField == nil || len(relation.foreignKeys) > 1 {\n\t\t\t\tschema.err = fmt.Errorf(\"invalid polymorphic foreign keys %+v for %v on field %s\", relation.foreignKeys,","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/schema/relationship.go#L205-L241","documentation":"A field tagged `gorm:\"polymorphic:...\"` requires the related (FieldSchema) struct to contain a `<polymorphic>Type` string column. GORM looks up `FieldsByName[typeName]` and, when the field does not exist, records this error. The polymorphic ID column (`<polymorphic>ID`) is checked separately.","triggerScenarios":"Declaring `Children []Cat `gorm:\"polymorphic:Owner\"`` on a User model while the Cat struct lacks an `OwnerType string` field; or misspelling the POLYMORPHIC tag value so the derived name (e.g. `petType`) matches nothing in the related struct.","commonSituations":"Renaming the polymorphic tag value without adding matching Type/ID fields to the target model; overriding the type field name via POLYMORPHICTYPE tag with a typo; adding polymorphic relations to a shared/embedded base struct where the type column lives elsewhere.","solutions":["Add the missing `<Poly>Type string` field to the related struct (for `polymorphic:Owner` that is `OwnerType string`).","If the column exists under a different name, point GORM at it with the POLYMORPHICTYPE tag setting.","Verify the tag value spelling matches the prefix used for the Type and ID columns.","Check the error message tail: it names the exact missing field (`...Type` here)."],"exampleFix":"// before\ntype User struct {\n    ID       uint\n    Children []Cat `gorm:\"polymorphic:Owner\"`\n}\ntype Cat struct {\n    ID       uint\n    OwnerID  int\n    // OwnerType missing\n}\n\n// after\ntype Cat struct {\n    ID         uint\n    OwnerID    int\n    OwnerType  string\n}","handlingStrategy":"validation","validationCode":"// Verify polymorphic type column exists on the related model\n_, err := schema.Parse(&Cat{}, &sync.Map{}, schema.NamingStrategy{})\n// or check directly:\nif !hasField(&Cat{}, \"OwnerType\") { // for polymorphic:Owner\n    panic(\"Cat must declare OwnerType string for polymorphic:Owner\")\n}","typeGuard":null,"tryCatchPattern":"if err := db.AutoMigrate(models...); err != nil {\n    if strings.Contains(err.Error(), \"missing field\") {\n        return fmt.Errorf(\"polymorphic model missing Type/ID column: %w\", err)\n    }\n    return err\n}","preventionTips":["Always create the pair `<Poly>Type string` + `<Poly>ID <keytype>` together on the related model.","Write a model-convention test asserting polymorphic tag values have matching columns.","Prefer letting GORM's convention names over custom POLYMORPHICTYPE overrides unless necessary."],"tags":["gorm","polymorphic","relation","schema"],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}