{"id":"77f910196a4b08c9","repo":"typeorm/typeorm","slug":"relation-entitytarget-relationpropertypath","errorCode":null,"errorMessage":"Relation \"${entityTarget}#${relationPropertyPath}\" does not have a many-to-many relationship.You can use this method only on many-to-many relations.","messagePattern":"Relation \"(.+?)#(.+?)\" does not have a many-to-many relationship\\.You can use this method only on many-to-many relations\\.","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"src/data-source/DataSource.ts","lineNumber":635,"sourceCode":"     * Gets entity metadata of the junction table (many-to-many table).\n     *\n     * @param entityTarget\n     * @param relationPropertyPath\n     */\n    getManyToManyMetadata(\n        entityTarget: EntityTarget<any>,\n        relationPropertyPath: string,\n    ) {\n        const relationMetadata =\n            this.getMetadata(entityTarget).findRelationWithPropertyPath(\n                relationPropertyPath,\n            )\n        if (!relationMetadata)\n            throw new TypeORMError(\n                `Relation \"${relationPropertyPath}\" was not found in ${entityTarget} entity.`,\n            )\n        if (!relationMetadata.isManyToMany)\n            throw new TypeORMError(\n                `Relation \"${entityTarget}#${relationPropertyPath}\" does not have a many-to-many relationship.` +\n                    `You can use this method only on many-to-many relations.`,\n            )\n\n        return relationMetadata.junctionEntityMetadata\n    }\n\n    /**\n     * Creates an Entity Manager for the current connection with the help of the EntityManagerFactory.\n     *\n     * @param queryRunner\n     */\n    createEntityManager(queryRunner?: QueryRunner): EntityManager {\n        return new EntityManagerFactory().create(this, queryRunner)\n    }\n\n    // -------------------------------------------------------------------------\n    // Protected Methods","sourceCodeStart":617,"sourceCodeEnd":653,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/data-source/DataSource.ts#L617-L653","documentation":"Thrown by DataSource.getManyToManyMetadata() when the resolved relation exists but `relationMetadata.isManyToMany` is false. The junction-table lookup only makes sense for many-to-many relations, so a @ManyToOne/@OneToMany/@OneToOne is rejected.","triggerScenarios":"Calling getManyToManyMetadata(Entity, prop) where prop is decorated with @ManyToOne, @OneToMany, or @OneToOne rather than @ManyToMany.","commonSituations":"Wrong API for the relation type (looking up a junction table that doesn't exist for non-M:N relations); refactoring a relation from M:N to M:1 without updating the metadata call.","solutions":["Confirm the relation is decorated @ManyToMany; if not, use the appropriate metadata/repository API for its type.","Switch the entity relation to @ManyToMany if a junction table is genuinely intended.","Use getMetadata(...).relations to inspect the relation type before calling."],"exampleFix":"// before\n@Entity() class User { @ManyToOne(() => Dept) dept!: Dept }\nds.getManyToManyMetadata(User, 'dept') // throws\n\n// after — use the relation as-is, not junction lookup\nds.getMetadata(User).findRelationWithPropertyPath('dept')","handlingStrategy":"validation","validationCode":"const rel = dataSource.getMetadata(Entity).findRelationWithPropertyPath(path)\nif (!rel || !rel.isManyToMany) {\n  throw new Error(`Relation ${Entity.name}#${path} is not many-to-many`)\n}\nreturn dataSource.getManyToManyMetadata(Entity, path)","typeGuard":"function isManyToManyRelation(ds: DataSource, target: EntityTarget<any>, path: string): boolean {\n  const r = ds.getMetadata(target).findRelationWithPropertyPath(path)\n  return !!r && r.isManyToMany\n}","tryCatchPattern":"try {\n  return dataSource.getManyToManyMetadata(Entity, path)\n} catch (e) {\n  if (e instanceof TypeORMError && /many-to-many/.test(e.message)) { /* use non-junction API for this relation type */ throw e }\n  else throw e\n}","preventionTips":["Only call getManyToManyMetadata on @ManyToMany relations.","Inspect relationMetadata.isManyToMany before the call.","Update callers when a relation type changes."],"tags":["metadata","relations","many-to-many","configuration"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}