go-gorm/gorm · error

failed to find relation: %s

Error message

failed to find relation: %s

What it means

Error "failed to find relation: %s" thrown in go-gorm/gorm.

Source

Thrown at gorm.go:505

		modelSchema, joinSchema *schema.Schema
	)

	err := stmt.Parse(model)
	if err != nil {
		return err
	}
	modelSchema = stmt.Schema

	err = stmt.Parse(joinTable)
	if err != nil {
		return err
	}
	joinSchema = stmt.Schema

	relation, ok := modelSchema.Relationships.Relations[field]
	isRelation := ok && relation.JoinTable != nil
	if !isRelation {
		return fmt.Errorf("failed to find relation: %s", field)
	}

	for _, ref := range relation.References {
		f := joinSchema.LookUpField(ref.ForeignKey.DBName)
		if f == nil {
			return fmt.Errorf("missing field %s for join table", ref.ForeignKey.DBName)
		}

		f.DataType = ref.ForeignKey.DataType
		f.GORMDataType = ref.ForeignKey.GORMDataType
		if f.Size == 0 {
			f.Size = ref.ForeignKey.Size
		}
		ref.ForeignKey = f
	}

	for name, rel := range relation.JoinTable.Relationships.Relations {
		if _, ok := joinSchema.Relationships.Relations[name]; !ok {

View on GitHub (pinned to 1d6ce99528)

Solutions

  1. Verify the relation name passed to SetupJoinTable/Association exists on the model.
  2. Ensure the schema was parsed (db.Model(&Model{})) before looking up the relation.
  3. Check the relationship tags (foreignKey, references) so the relation is registered.

Example fix

Verify the relation field name matches the struct field, e.g. db.Association("Orders") where the field is Orders []Order.

When it happens

Trigger: Thrown at gorm.go:505 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/21c34ebe0ce8fb86. Report an issue: GitHub.