{"record":{"id":"067a006cb46680e7","repo":"go-gorm/gorm","slug":"invalid-polymorphic-type-v-for-v-on-field-s-mi-067a00","errorCode":null,"errorMessage":"invalid polymorphic type %v for %v on field %s, missing primaryKey field","messagePattern":"invalid polymorphic type (.+?) for (.+?) on field (.+?), missing primaryKey field","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"schema/relationship.go","lineNumber":247,"sourceCode":"\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,\n\t\t\t\t\tschema, field.Name)\n\t\t\t}\n\t\t}\n\n\t\tif primaryKeyField == nil {\n\t\t\tschema.err = fmt.Errorf(\"invalid polymorphic type %v for %v on field %s, missing primaryKey field\",\n\t\t\t\trelation.FieldSchema, schema, field.Name)\n\t\t\treturn\n\t\t}\n\n\t\t// use same data type for foreign keys\n\t\tif copyableDataType(primaryKeyField.DataType) {\n\t\t\trelation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType\n\t\t}\n\t\trelation.Polymorphic.PolymorphicID.GORMDataType = primaryKeyField.GORMDataType\n\t\tif relation.Polymorphic.PolymorphicID.Size == 0 {\n\t\t\trelation.Polymorphic.PolymorphicID.Size = primaryKeyField.Size\n\t\t}\n\n\t\trelation.References = append(relation.References, &Reference{\n\t\t\tPrimaryKey:    primaryKeyField,\n\t\t\tForeignKey:    relation.Polymorphic.PolymorphicID,\n\t\t\tOwnPrimaryKey: true,\n\t\t})","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/schema/relationship.go#L229-L265","documentation":"After resolving the polymorphic foreign key, GORM needs a non-nil primary key field on the owner schema (either the tagged foreignKeys[0] or schema.PrioritizedPrimaryField). If the owner struct has no primary key at all (no `gorm:\"primaryKey\"` and no conventional ID field), primaryKeyField stays nil and this error is returned.","triggerScenarios":"Owner model lacks any primary key: no field named ID, no `gorm:\"primaryKey\"` tag, and no DeletedAt-style prioritized field; or the foreignKeys lookup silently failed in a prior branch leaving the variable nil.","commonSituations":"Join/view structs used as polymorphic owners without a key; composite-key structs where every key column is tagged differently; embedding models where the key lives in an unexported field.","solutions":["Define a primary key on the owner struct: `ID uint `gorm:\"primaryKey\"``.","Or tag an existing unique column with `gorm:\"primaryKey\"`.","If foreignKeys was specified, verify the referenced field exists on the owner (not the related) schema."],"exampleFix":"// before\ntype Owner struct {\n    Name string // no primary key\n    Pets []Pet `gorm:\"polymorphic:Owner\"`\n}\n\n// after\ntype Owner struct {\n    ID   uint `gorm:\"primaryKey\"`\n    Name string\n    Pets []Pet `gorm:\"polymorphic:Owner\"`\n}","handlingStrategy":"validation","validationCode":"// Ensure owner has a primary key before declaring polymorphic children\nif _, err := schema.Parse(&Owner{}, &sync.Map{}, schema.NamingStrategy{}); err != nil {\n    return err // or inspect .PrioritizedPrimaryField == nil","typeGuard":null,"tryCatchPattern":"if err := db.AutoMigrate(&Owner{}, &Pet{}); err != nil {\n    if strings.Contains(err.Error(), \"missing primaryKey field\") {\n        return fmt.Errorf(\"owner model needs a primary key: %w\", err)\n    }\n    return err\n}","preventionTips":["Give every owner model an explicit `gorm:\"primaryKey\"` field.","Do not use view/DTO structs as relation owners.","Include owner models in AutoMigrate so key errors surface early."],"tags":["gorm","polymorphic","primary-key","relation"],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}