{"record":{"id":"bf60404f338d48dd","repo":"go-gorm/gorm","slug":"unsupported-relations-s","errorCode":null,"errorMessage":"unsupported relations: %s","messagePattern":"unsupported relations: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"association.go","lineNumber":30,"sourceCode":"\n// Association Mode contains some helper methods to handle relationship things easily.\ntype Association struct {\n\tDB           *DB\n\tRelationship *schema.Relationship\n\tUnscope      bool\n\tError        error\n}\n\nfunc (db *DB) Association(column string) *Association {\n\tassociation := &Association{DB: db, Unscope: db.Statement.Unscoped}\n\ttable := db.Statement.Table\n\n\tif association.Error = db.Statement.Parse(db.Statement.Model); association.Error == nil {\n\t\tdb.Statement.Table = table\n\t\tassociation.Relationship = db.Statement.Schema.Relationships.Relations[column]\n\n\t\tif association.Relationship == nil {\n\t\t\tassociation.Error = fmt.Errorf(\"%w: %s\", ErrUnsupportedRelation, column)\n\t\t}\n\n\t\tdb.Statement.ReflectValue = reflect.ValueOf(db.Statement.Model)\n\t\tfor db.Statement.ReflectValue.Kind() == reflect.Ptr {\n\t\t\tdb.Statement.ReflectValue = db.Statement.ReflectValue.Elem()\n\t\t}\n\t}\n\n\treturn association\n}\n\nfunc (association *Association) Unscoped() *Association {\n\treturn &Association{\n\t\tDB:           association.DB,\n\t\tRelationship: association.Relationship,\n\t\tError:        association.Error,\n\t\tUnscope:      true,\n\t}","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/association.go#L12-L48","documentation":"db.Association(column) parses the model and looks up db.Statement.Schema.Relationships.Relations[column]. When the named column is not a relation (typo, plain field, or missing embedded relation), it wraps ErrUnsupportedRelation: fmt.Errorf(\"%w: %s\", ErrUnsupportedRelation, column). The Association object carries this error in association.Error.","triggerScenarios":"Calling db.Model(&user).Association(\"Ordr\") (typo), Association(\"Name\") where Name is a string column not a relation, or Association on a model whose relation field is tagged `gorm:\"-\"` so it is excluded from Relationships.","commonSituations":"Renaming a relation field but not updating the string in service code (no compile-time check on these strings); using a column name instead of the Go field name; embedding structs so the relation lives at a different name; copy-paste between models.","solutions":["Use the exact Go field name of the relation: Association(\"Orders\"), not the column or table name.","Check assoc.Error immediately after obtaining the Association handle before calling Append/Replace/etc.","If the field is genuinely a relation but not recognized, verify it is not ignored (`gorm:\"-\"`) and its type is a struct/slice of struct/pointer.","Centralize association-name strings as constants next to the model."],"exampleFix":"// before\nassoc := db.Model(&user).Association(\"orders\") // typo -> ErrUnsupportedRelation: orders\n\n// after\nconst AssocOrders = \"Orders\"\nassoc := db.Model(&user).Association(AssocOrders)\nif assoc.Error != nil { return assoc.Error }","handlingStrategy":"validation","validationCode":"func relationExists(db *gorm.DB, model interface{}, name string) bool {\n    stmt := &gorm.Statement{DB: db}\n    if err := stmt.Parse(model); err != nil { return false }\n    _, ok := stmt.Schema.Relationships.Relations[name]\n    return ok\n}\n// before:\nif !relationExists(db, &User{}, \"Orders\") { return errors.New(\"bad association\") }","typeGuard":null,"tryCatchPattern":"assoc := db.Model(&user).Association(\"Orders\")\nif assoc.Error != nil {\n    if errors.Is(assoc.Error, gorm.ErrUnsupportedRelation) { /* wrong name */ }\n    return assoc.Error\n}","preventionTips":["Check assoc.Error right after db.Model(...).Association(name) - it is the API's error channel.","Define association-name constants beside models instead of inline strings.","Add a startup test asserting every association name used in code resolves."],"tags":["gorm","associations","api-misuse","typo"],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}