go-gorm/gorm · error
invalid polymorphic type %v for %v on field %s, missing fiel
Error message
invalid polymorphic type %v for %v on field %s, missing field %s
What it means
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.
Source
Thrown at schema/relationship.go:223
)
if value, ok := field.TagSettings["POLYMORPHICTYPE"]; ok {
typeName = strings.TrimSpace(value)
}
if value, ok := field.TagSettings["POLYMORPHICID"]; ok {
typeId = strings.TrimSpace(value)
}
relation.Polymorphic.PolymorphicType = relation.FieldSchema.FieldsByName[typeName]
relation.Polymorphic.PolymorphicID = relation.FieldSchema.FieldsByName[typeId]
if value, ok := field.TagSettings["POLYMORPHICVALUE"]; ok {
relation.Polymorphic.Value = strings.TrimSpace(value)
}
if relation.Polymorphic.PolymorphicType == nil {
schema.err = fmt.Errorf("invalid polymorphic type %v for %v on field %s, missing field %s",
relation.FieldSchema, schema, field.Name, polymorphic+"Type")
}
if relation.Polymorphic.PolymorphicID == nil {
schema.err = fmt.Errorf("invalid polymorphic type %v for %v on field %s, missing field %s",
relation.FieldSchema, schema, field.Name, polymorphic+"ID")
}
if schema.err == nil {
relation.References = append(relation.References, &Reference{
PrimaryValue: relation.Polymorphic.Value,
ForeignKey: relation.Polymorphic.PolymorphicType,
})
primaryKeyField := schema.PrioritizedPrimaryField
if len(relation.foreignKeys) > 0 {
if primaryKeyField = schema.LookUpField(relation.foreignKeys[0]); primaryKeyField == nil || len(relation.foreignKeys) > 1 {
schema.err = fmt.Errorf("invalid polymorphic foreign keys %+v for %v on field %s", relation.foreignKeys,View on GitHub (pinned to 1d6ce99528)
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).
Example fix
// before
type User struct {
ID uint
Children []Cat `gorm:"polymorphic:Owner"`
}
type Cat struct {
ID uint
OwnerID int
// OwnerType missing
}
// after
type Cat struct {
ID uint
OwnerID int
OwnerType string
} Defensive patterns
Strategy: validation
Validate before calling
// Verify polymorphic type column exists on the related model
_, err := schema.Parse(&Cat{}, &sync.Map{}, schema.NamingStrategy{})
// or check directly:
if !hasField(&Cat{}, "OwnerType") { // for polymorphic:Owner
panic("Cat must declare OwnerType string for polymorphic:Owner")
} Try / catch
if err := db.AutoMigrate(models...); err != nil {
if strings.Contains(err.Error(), "missing field") {
return fmt.Errorf("polymorphic model missing Type/ID column: %w", err)
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- failed to parse field: %s, error: %w
- unsupported data type %v for %v on field %s
- invalid polymorphic foreign keys %+v for %v on field %s
- invalid polymorphic type %v for %v on field %s, missing prim
- unsupported relationship
AI-assisted analysis of go-gorm/gorm@1d6ce99528 (2026-08-15).
Data as JSON: /api/errors/307083096d333835.
Report an issue: GitHub.