go-gorm/gorm · error

missing field %s for join table

Error message

missing field %s for join table

What it means

Error "missing field %s for join table" thrown in go-gorm/gorm.

Source

Thrown at gorm.go:511

	}
	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 {
			rel.Schema = joinSchema
			joinSchema.Relationships.Relations[name] = rel
		}
	}
	relation.JoinTable = joinSchema

View on GitHub (pinned to 1d6ce99528)

Solutions

  1. Add the missing field to the join table struct used in SetupJoinTable.
  2. Check that the join model includes both foreign key fields with correct gorm tags.
  3. Run AutoMigrate on the join model so its schema matches expectations.

Example fix

Define the join table with all required foreign key fields: type UserRole struct { UserID uint; RoleID uint }

When it happens

Trigger: Thrown at gorm.go:511 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/77489cec21b92b26. Report an issue: GitHub.