title: TypeORM: [364] ERROR: missing FROM-clause entry for table "userorganisation" at character 401
body: <p>I just hit a pretty interesting problem when using <code>TypeORM</code> and joining tables that I've set up for a <code>Postgres</code> database. I figured it out but thought I'd post the info here for anyone else that had a similar problem.</p>
  <p>I have 3 tables set up on my database: <code>user</code>, <code>organisation</code>, <code>user_organisation</code>.</p>
  <p>The idea for this is that a user can belong to many organisations and the table called <code>user_organisation</code> maps users to these organisations. So my entities look like this,</p>
  <p><strong>user.entity.ts</strong></p>
  <pre><code>import { TimestampedEntity } from '@shared/entities/timestamped.entity';
  import { Organisation } from '@user/entities/organisation.entity';
  import { UserOrganisation } from '@user/entities/user-organisation.entity';
  import { Column, Entity, Index, JoinTable, ManyToMany, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
  
  @Entity()
  @Index(['email', 'password'])
  export class User extends TimestampedEntity {
      @PrimaryGeneratedColumn()
      userId: number;
  
      @Column({
          length: 64
      })
      username: string;
  
      @Column({
          length: 500
      })
      email: string;
  
      @Column({
          length: 255
      })
      password: string;
  
      @Column({
          type: 'json',
      })
      details: any;
  
      @Column({
          nullable: true
      })
      refreshToken: string;
  
      @OneToMany(type =&gt; UserOrganisation, userOrganisation =&gt; userOrganisation.user)
      @JoinTable()
      userOrganisations: UserOrganisation[];
  }
  </code></pre>
  <p><strong>user-organisation.entity.ts</strong></p>
  <pre><code>import { Organisation } from '@user/entities/organisation.entity';
  import { User } from '@user/entities/user.entity';
  import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';
  
  @Entity()
  export class UserOrganisation {
      @ManyToOne(type =&gt; User, user =&gt; user.userOrganisations, { primary: true })
      user: User;
  
      @ManyToOne(type =&gt; Organisation, organisation =&gt; organisation.userOrganisations, { primary: true })
      organisation: Organisation;
  }
  </code></pre>
  <p><strong>organisation.entity.ts</strong></p>
  <pre><code>import { TimestampedEntity } from '@shared/entities/timestamped.entity';
  import { UserOrganisation } from '@user/entities/user-organisation.entity';
  import { User } from '@user/entities/user.entity';
  import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
  
  @Entity()
  export class Organisation extends TimestampedEntity {
      @PrimaryGeneratedColumn()
      orgId: number;
  
      @Column({
          length: 255
      })
      name: string;
  
      @Column({
          type: 'json'
      })
      details: any;
  
      @OneToMany(type =&gt; UserOrganisation, userOrganisation =&gt; userOrganisation.organisation)
      userOrganisations: UserOrganisation[];
  }
  </code></pre>
  <p>I was then trying to run the following query,</p>
  <pre><code>this.userRepository.createQueryBuilder('user')
      .where('user.email = :email', { email })
      .innerJoin(UserOrganisation, 'userOrganisation', 'user.userId = userOrganisation.userUserId')
      .getOne();
  </code></pre>
  <p>And this is the error message I got,</p>
  <pre><code>ERROR:  missing FROM-clause entry for table &quot;userorganisation&quot; at character 401
  </code></pre>
  <p>The final query printed out like this,</p>
  <pre><code>SELECT &quot;user&quot;.&quot;createdAt&quot; AS &quot;user_createdAt&quot;
   , &quot;user&quot;.&quot;updatedAt&quot; AS &quot;user_updatedAt&quot;
   , &quot;user&quot;.&quot;userId&quot; AS &quot;user_userId&quot;
   , &quot;user&quot;.&quot;username&quot; AS &quot;user_username&quot;
   , &quot;user&quot;.&quot;email&quot; AS &quot;user_email&quot;
   , &quot;user&quot;.&quot;password&quot; AS &quot;user_password&quot;
   , &quot;user&quot;.&quot;details&quot; AS &quot;user_details&quot;
   , &quot;user&quot;.&quot;refreshToken&quot; AS &quot;user_refreshToken&quot; 
   FROM &quot;user&quot; &quot;user&quot; 
   INNER JOIN &quot;user_organisation&quot; &quot;userOrganisation&quot; 
        ON      &quot;user&quot;.&quot;userId&quot; = userOrganisation.userUserId 
   WHERE &quot;user&quot;.&quot;email&quot; = $1
  </code></pre>
  <p>The way I fixed it is described below.</p>