go-gorm/gorm · error

relation %s not found

Error message

relation %s not found

What it means

Error "relation %s not found" thrown in go-gorm/gorm.

Source

Thrown at generics.go:501

		return db.Preload(association, func(tx *DB) *DB {
			q := preloadBuilder{db: tx.getInstance()}
			if query != nil {
				if err := query(&q); err != nil {
					db.AddError(err)
				}
			}

			relation, ok := db.Statement.Schema.Relationships.Relations[association]
			if !ok {
				if preloadFields := strings.Split(association, "."); len(preloadFields) > 1 {
					relationships := &db.Statement.Schema.Relationships
					for _, field := range preloadFields {
						var ok bool
						relation, ok = relationships.Relations[field]
						if ok {
							relationships = &relation.FieldSchema.Relationships
						} else {
							db.AddError(fmt.Errorf("relation %s not found", association))
							return nil
						}
					}
				} else {
					db.AddError(fmt.Errorf("relation %s not found", association))
					return nil
				}
			}

			if q.limitPerRecord > 0 {
				if relation.JoinTable != nil {
					tx.AddError(fmt.Errorf("many2many relation %s don't support LimitPerRecord", association))
					return tx
				}

				refColumns := []clause.Column{}
				for _, rel := range relation.References {
					if rel.OwnPrimaryKey {

View on GitHub (pinned to 1d6ce99528)

Solutions

  1. Check the relation name matches the struct field name exactly (case-sensitive).
  2. Ensure the related field is a struct or slice of structs so GORM parses it as a relation.
  3. Verify the model was migrated/parsed before the call so relationships are registered.

Example fix

Check the relation name against the model schema before Association(): if _, ok := stmt.Schema.Relationships.Relations[name]; !ok { return }

When it happens

Trigger: Thrown at generics.go:501 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of go-gorm/gorm@1d6ce99528 (2026-08-15). Data as JSON: /api/errors/a267d10b41e91dd1. Report an issue: GitHub.