title: How to save relation in @ManyToMany in typeORM
body: <p>There are 2 entities named <code>Article</code> and <code>Classification</code>. And the relation of them is <code>@ManyToMany</code>.</p>
  
  <p>Here's my question: How to save the relation?</p>
  
  <p>My code as below:</p>
  
  <pre><code>  @Entity()
      export class Article {
          @PrimaryGeneratedColumn()
          id: number;
  
          @Column()
          name: string;
  
          @CreateDateColumn()
          createTime: Date;
  
          @UpdateDateColumn()
          updateTime: Date;
  
          @Column({
              type: 'text',
          })
          content: string;
  
          @Column({
              default: 0,
          })
          likeAmount: number;
  
          @Column({
              default: 0,
          })
          commentAmount: number;
      }
  
      @Entity()
      export class Classification {
          @PrimaryGeneratedColumn()
          id: number;
  
          @CreateDateColumn()
          createTime: Date;
  
          @UpdateDateColumn()
          updateTime: Date;
  
          @Column()
          name: string;
  
          @ManyToMany(type =&gt; Article)
          @JoinTable()
          articles: Article[];
      }
  </code></pre>
  
  <p>I can save the <code>Article</code> and <code>Classification</code> successful. But I'm not sure how to save the relation of them.</p>
  
  <p>I have tried to save the relation via below code:</p>
  
  <pre><code>async create(dto: ArticleClassificationDto): Promise&lt;any&gt; {
      const article = this.repository.save(dto);
      article.then(value =&gt; {
        console.log(value);//console the object article
        value.classification.forEach(item =&gt; {
          const classification = new Classification();
          classification.id = item.id;
          classification.articles = [];
          classification.articles.push(value);
          this.classificationService.save(classification);
        })
      });
      console.log(article);
      return null;
    }
  </code></pre>
  
  <p>And the post data strcture like that</p>
  
  <pre><code>    {
          "name":"artile name",
          "content":"article content",
          "classification":[{
              "id":4
          },{
              "id":3
          }]
      }
  </code></pre>
  
  <p>At the beginning, it works.</p>
  
  <p><a href="https://i.stack.imgur.com/7yrPB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7yrPB.png" alt="enter image description here"></a></p>
  
  <p>But when I post the data again, the old record was replaced rather create another record.</p>
  
  <p><a href="https://i.stack.imgur.com/HBFlc.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HBFlc.png" alt="enter image description here"></a></p>
  
  <p>What should I do next?</p>
  
  <p>Just look below code please.</p>
  
  <pre><code>async create(dto: ArticleClassificationDto): Promise&lt;any&gt; {
      this.repository.save(dto).then(article =&gt; {
        article.classification.forEach(item =&gt; {
          this.ClassificationRepository.findOne(
            {
              // the privous method is get all the articles from databse and push into this array
              // relations: ['articles'],
              where: { id: item }// now I change the data strcture, just contains id instead of {id}
            }
          ).then(classification =&gt; {
            // console.log(article);
            console.log(classification);
            // cmd will show ' UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined' withous below line code. But if I init the array manually,the old record will be replaced again.
            // classification.articles = [];
            classification.articles.push(article);
            this.ClassificationRepository.save(classification);
          });
        })
      })
      return null;
    }
  </code></pre>