go-gorm/gorm · error

many2many relation %s don't support LimitPerRecord

Error message

many2many relation %s don't support LimitPerRecord

What it means

Error "many2many relation %s don't support LimitPerRecord" thrown in go-gorm/gorm.

Source

Thrown at generics.go:513

					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 {
						refColumns = append(refColumns, clause.Column{Name: rel.ForeignKey.DBName})
					}
				}

				if len(refColumns) != 0 {
					selectExpr := clause.CommaExpression{}
					for _, column := range q.db.Statement.Selects {
						selectExpr.Exprs = append(selectExpr.Exprs, clause.Expr{SQL: "?", Vars: []interface{}{clause.Column{Name: column}}})
					}

					if len(selectExpr.Exprs) == 0 {
						selectExpr.Exprs = []clause.Expression{clause.Expr{SQL: "*", Vars: []interface{}{}}}

View on GitHub (pinned to 1d6ce99528)

Solutions

  1. Remove LimitPerRecord (Preload with limit per record) for many2many relations.
  2. Load the many2many records in a separate query and apply the limit manually.
  3. Restructure the schema to has-many through an explicit join model if per-record limits are needed.

Example fix

Avoid LimitPerRecord on many2many preloads; preload the join table separately and limit in application code.

When it happens

Trigger: Thrown at generics.go:513 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/5a05be23810a000e. Report an issue: GitHub.