{"record":{"id":"5a05be23810a000e","repo":"go-gorm/gorm","slug":"many2many-relation-s-don-t-support-limitperrecord","errorCode":null,"errorMessage":"many2many relation %s don't support LimitPerRecord","messagePattern":"many2many relation (.+?) don't support LimitPerRecord","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"generics.go","lineNumber":513,"sourceCode":"\t\t\t\t\tfor _, field := range preloadFields {\n\t\t\t\t\t\tvar ok bool\n\t\t\t\t\t\trelation, ok = relationships.Relations[field]\n\t\t\t\t\t\tif ok {\n\t\t\t\t\t\t\trelationships = &relation.FieldSchema.Relationships\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdb.AddError(fmt.Errorf(\"relation %s not found\", association))\n\t\t\t\t\t\t\treturn nil\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tdb.AddError(fmt.Errorf(\"relation %s not found\", association))\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif q.limitPerRecord > 0 {\n\t\t\t\tif relation.JoinTable != nil {\n\t\t\t\t\ttx.AddError(fmt.Errorf(\"many2many relation %s don't support LimitPerRecord\", association))\n\t\t\t\t\treturn tx\n\t\t\t\t}\n\n\t\t\t\trefColumns := []clause.Column{}\n\t\t\t\tfor _, rel := range relation.References {\n\t\t\t\t\tif rel.OwnPrimaryKey {\n\t\t\t\t\t\trefColumns = append(refColumns, clause.Column{Name: rel.ForeignKey.DBName})\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif len(refColumns) != 0 {\n\t\t\t\t\tselectExpr := clause.CommaExpression{}\n\t\t\t\t\tfor _, column := range q.db.Statement.Selects {\n\t\t\t\t\t\tselectExpr.Exprs = append(selectExpr.Exprs, clause.Expr{SQL: \"?\", Vars: []interface{}{clause.Column{Name: column}}})\n\t\t\t\t\t}\n\n\t\t\t\t\tif len(selectExpr.Exprs) == 0 {\n\t\t\t\t\t\tselectExpr.Exprs = []clause.Expression{clause.Expr{SQL: \"*\", Vars: []interface{}{}}}","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/generics.go#L495-L531","documentation":"Error \"many2many relation %s don't support LimitPerRecord\" thrown in go-gorm/gorm.","triggerScenarios":"Thrown at generics.go:513 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove LimitPerRecord (Preload with limit per record) for many2many relations.","Load the many2many records in a separate query and apply the limit manually.","Restructure the schema to has-many through an explicit join model if per-record limits are needed."],"exampleFix":"Avoid LimitPerRecord on many2many preloads; preload the join table separately and limit in application code.","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}