title: How to insert an entity with OneToMany relation in NestJS?
body: <p>There is the description how to do this in typeorm official docs <a href="https://typeorm.io/#/many-to-one-one-to-many-relations" rel="nofollow noreferrer">https://typeorm.io/#/many-to-one-one-to-many-relations</a>. But I can't do the same in NestJS with <code>Repository</code> and <code>insert</code> method.</p>
  <p>I have written these entities (other columns were omitted)</p>
  <pre class="lang-js prettyprint-override"><code>    @Entity()
      export class News {
        @OneToMany(type =&gt; NewsImage, image =&gt; image.news)
        public images: NewsImage[];
      }
      
      @Entity()
      export class NewsImage {
        @ManyToOne(type =&gt; News, news =&gt; news.images)
        public news: News;
      }
  </code></pre>
  <p>I have tried something like this</p>
  <pre class="lang-js prettyprint-override"><code>    function first() {
        const news = new News();
        const image = new NewsImage();
        news.images = [ image ];
        return from(this.newsRepo.insert(news))
          .pipe(
            switchMap(() =&gt; this.imageRepo.insert(image)),
          );
      }
      
      function second() {
        const news = new News();
        const image = new NewsImage();
        image.news = news;
        return from(this.imageRepo.insert(image))
          .pipe(
            switchMap(() =&gt; this.newsRepo.insert(news)),
          )
      }
  </code></pre>
  <p>It inserts news and image, but image's <code>newsId</code> is <code>null</code>.</p>