title: TypeORM: how to implement bidirectional relationship, multiple fields --> one entity type
body: <p>I've created a 'document' entity: </p>
  
  <p>e.g.</p>
  
  <pre><code>@Entity()
  export class Document {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    name: string;
  
    @Column()
    path: string;
     ...
  
  }
  </code></pre>
  
  <p>Multiple documents can be related to different entity types: post, userProfile etc  </p>
  
  <p>in the post entity for example, I have several fields which all specify document relationships. </p>
  
  <pre><code>  @OneToOne(type =&gt; DocumentEntity)
    @JoinColumn({ name: 'default_document' })
    defaultDocument: DocumentEntity;
  
    @OneToOne(type =&gt; DocumentEntity)
    @JoinColumn({ name: 'featured_document' })
    featuredDocument: DocumentEntity;
  
    @OneToMany(type =&gt; DocumentEntity, document =&gt; document.post)
    @JoinColumn({ name: 'other_documents' })
    otherDocs: DocumentEntity[]; 
  </code></pre>
  
  <p>I'm unclear how to make the document relationships bidirectional. 
  I had hoped to have a single field on document like:</p>
  
  <pre><code>  @ManyToOne(type =&gt; abstractEntity, entity =&gt; entity.document)
    parentEntity: abstractEntity;
  </code></pre>
  
  <p>This way if I'm querying document entities for their parent relationships, 
  I would have a result like:</p>
  
  <pre><code>documents: [
  {
  id: 1,
  name: 'document 1', 
  path: 'https://image.hosted.service/1.jpg', 
  parentEntityId: 23
  }, 
  {
  id: 2
  name: 'document 2', 
  path: 'https://image.hosted.service/2.jpg'
  parentEntityId: 27
  }
  ] 
  </code></pre>
  
  <p>But Typeorm seems to want me to define an exact matching field for each parent relationship field on documentEntity like: </p>
  
  <pre><code>@Entity()
  export class Document {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    name: string;
  
    @Column()
    path: string;
    ...
  
    @OneToOne(type =&gt; PostEntity, post =&gt; post.defaultDocument)
    postEntityDefaultDoc: PostEntity;
  
    @OneToOne(type =&gt; PostEntity, post =&gt; post.featuredDocument)
    postEntityFeaturedDoc: PostEntity;
  
    @ManyToOne(type =&gt; PostEntity, post =&gt; post.otherDocs)
    otherDocs: PostEntity[];
  
  
  }
  </code></pre>
  
  <p>For the sake of simplicity in this example, there are no M:N relationships: document can have at most one parent. </p>
  
  <p>It doesn't seem correct that I would have to define a new field on document entity, for every possible instance where a parent entity field references a document. 
  A query on document would not return a list with one field defining the parent entity, instead I have to parse/aggregate an arbitrary number of fields.</p>
  
  <p>I can't seem to find any tutorials/examples in which a single entity has many fields each referencing the same other entity, which is making me think my basic approach is flawed.  </p>