title: Many to Many Joins in TypeORM
body: <p>We are currently working on implementing NestJS against our DB. We decided to use TypeORM to give us a strong ORM to use for most of our basic to intermediate queries. However, I am having issues setting up one particular relationship in our entities.</p>
  <p>Our table structure comes from an existing system that cannot be modified. It is as follows:</p>
  <p><strong>Employee Table</strong></p>
  <pre><code>+-----------+-----------+---------------+
  | HRCO (PK) | HRRef(PK) | udDisplayName |
  +-----------+-----------+---------------+
  |         2 |       323 | John          |
  |         2 |       500 | Larry         |
  |         1 |        29 | Jim           |
  +-----------+-----------+---------------+
  </code></pre>
  <pre><code>**Skill Table**
  +----------+----------+----------------+
  | HRCo(PK) | Code(PK) |  Description   |
  +----------+----------+----------------+
  |        2 | CODE1    | Test Class     |
  |        2 | CODE2    | Crane Training |
  |        1 | CODE1    | Truck Training |
  +----------+----------+----------------+
  </code></pre>
  <pre><code>**Join Table - EmployeeSkills**
  +-----------+---------------+-----------+------------+
  | HRCO (FK) | Employee (FK) | Code (FK) |  Expires   |
  +-----------+---------------+-----------+------------+
  |         2 |           323 | CODE1     | 2019-01-01 |
  |         2 |           323 | CODE2     | 2020-01-01 |
  |         1 |            29 | CODE1     | 2020-01-01 |
  +-----------+---------------+-----------+------------+
  </code></pre>
  <p>I recognize this is a many-to-many relationship that has a composite foreign key. Most of the NestJS docs lead you down using the ManyToMany and OneToMany x 2 pattern of establishing the relationship. However, those seem to only work if the joiner table has one key for each table rather than a composite.</p>
  <p>Right now my classes look like the following:</p>
  <p><strong>Skills</strong></p>
  
  <pre class="lang-ts prettyprint-override"><code>import { Entity, Column, PrimaryColumn, ManyToOne, OneToMany } from &quot;typeorm&quot;;
  import { EmployeeToSkillEntity } from &quot;../employee-to-skill/employee-skill.entity&quot;;
  
  @Entity({name:&quot;skills&quot;})
  export class SkillEntity {
  
    @PrimaryColumn({ name: &quot;HRCo&quot; })
    company: number;
  
    @PrimaryColumn({ name: &quot;Code&quot; })
    code: number;
  
   
  
    @Column({name:&quot;Description&quot;})
    description: Date;
  
    @OneToMany(type =&gt; EmployeeToSkillEntity, employeeToSkill =&gt; employeeToSkill.code)
    employeeToSkills: EmployeeToSkillEntity[]
  
  }
  
  </code></pre>
  <p><strong>Employee</strong></p>
  <pre class="lang-ts prettyprint-override"><code>import { Entity, Column, PrimaryColumn, OneToMany } from &quot;typeorm&quot;;
  import { EmployeeToSkillEntity } from &quot;../employee-to-skill/employee-skill.entity&quot;;
  
  /**
   * @ignore
   */
  @Entity({name:&quot;employee&quot;})
  export class EmployeeEntity {
    @PrimaryColumn({ name: &quot;HRCo&quot; })
    company: number;
  
    @PrimaryColumn({ name: &quot;HRRef&quot; })
    employeeNumber: number;
  
    @Column({name: &quot;udDisplayName&quot;})
    displayName: string;
  
    @OneToMany(type =&gt; EmployeeToSkillEntity, employeeToSkill =&gt; employeeToSkill.employee)
    employeeToSkills: EmployeeToSkillEntity[]
  
  }
  
  </code></pre>
  <pre class="lang-ts prettyprint-override"><code>import { Entity, Column, PrimaryColumn, ManyToOne, PrimaryGeneratedColumn } from &quot;typeorm&quot;;
  import { EmployeeEntity } from &quot;../employee/employee.entity&quot;;
  import { SkillEntity } from &quot;../skill/skill.entity&quot;;
  
  /**
   * @ignore
   */
  @Entity({ name: &quot;employeeskills&quot; })
  export class EmployeeToSkillEntity {
    @PrimaryColumn({ name: &quot;HRCo&quot; })
    companyNumber: number;
  
    @PrimaryColumn({ name: &quot;HRRef&quot; })
    employeeNumber: number;
  
    @PrimaryColumn({ name: &quot;Code&quot; })
    code: string;
  
    @Column({ name: &quot;CertDate&quot; })
    certDate: Date;
  
    @Column({ name: &quot;ExpireDate&quot; })
    expireDate: Date;
  
    @Column({ name: &quot;SkillTester&quot; })
    skillTester: string;
  
    @Column({ name: &quot;HistSeq&quot; })
    histSeq: string;
  
    @Column({ name: &quot;Notes&quot; })
    notes: string;
  
    @Column({ name: &quot;UniqueAttchID&quot; })
    attachmentID: number;
  
    @Column({ name: &quot;Type&quot; })
    type: string;
  
    @Column({ name: &quot;KeyID&quot; })
    keyID: number;
  
    @Column({ name: &quot;udLastModDate&quot; })
    lastModifiedDate: Date;
  
    @Column({ name: &quot;udLicense&quot; })
    license: number;
  
    @ManyToOne(type =&gt; EmployeeEntity, (employee) =&gt; employee.employeeToSkills)
    @JoinColumn([{ name: &quot;HRCo&quot;, referencedColumnName: &quot;companyNumber&quot; }, { name: &quot;HRRef&quot;, referencedColumnName: &quot;employeeNumber&quot; }])
    employee: EmployeeEntity;
  
    @ManyToOne(type =&gt; SkillEntity, (skill) =&gt; skill.employeeToSkills)
    @JoinColumn([{ name: &quot;HRCo&quot;, referencedColumnName: &quot;companyNumber&quot; }, { name: &quot;Code&quot;, referencedColumnName: &quot;code&quot; }])
    skill: SkillEntity;
  }
  </code></pre>
  <p>However, I am getting an error on query generation because I am getting columns for the relationship + the property name like &quot;skillCode&quot;.</p>
  <p>Any help would be appreciated.</p>
  <p>Thanks</p>