title: How update entity if exist or create if not exist entity
body: <p>Here are my entities.</p>
  
  <pre><code>// user
  
  @PrimaryGeneratedColumn()
  public id: number;
  
  @Column({ type: 'varchar', nullable: false })
  public email: string;
  
  @Column({ type: 'varchar', nullable: false })
  public password: string;
  
  @OneToOne(() =&gt; Token, (token: Token) =&gt; token.user)
  public token: Token;
  </code></pre>
  
  <pre><code>// token
  
  @PrimaryGeneratedColumn()
  public id: number;
  
  @Column({ type: 'varchar', nullable: false })
  public uuid: string;
  
  @Column({ type: 'integer', nullable: false })
  public userId: number;
  
  @OneToOne(() =&gt; User, (user: User) =&gt; user.hash, { cascade: ['insert', 'remove'] })
  @JoinColumn({ name: 'userId' })
  public user: User;
  </code></pre>
  
  <p>This is how I save current in my database.</p>
  
  <pre><code>private async savePayload(tokenDto: CreateTokenDto) {
      const token = this.tokenRepository.create(tokenDto);
      return await this.tokenRepository.save(token);
  }
  </code></pre>
  
  <p>When I first save my token to the database, all of it is saved. </p>
  
  <p>When I save a second time, I get an error.</p>
  
  <blockquote>
    <p>ER_DUP_ENTRY: Duplicate entry '36' for key 'REL_d417e5d35f2434afc4bd48cb4d'</p>
  </blockquote>
  
  <p>I read in the documentation about the save method. But why I get the error, I can not understand. I expect the record to be updated. Why are my token details not updated?</p>
  
  <p>I understand how to do this using sql.</p>
  
  <pre><code>INSERT INTO "Tokens" (UUID, USERID)
      VALUES ('d93ab036-768c-420a-98d6-2f80c79e6ae7', 36)
          ON CONFLICT (USERID) 
              DO UPDATE SET UUID = 'd93ab036-768c-420a-98d6-2f80c79e6ae7', 
                  USERID = 36'
  </code></pre>
  
  <p>After some experiments, I noticed that when I specify the token id, then saving or updating is successful.</p>
  
  <pre><code>private async savePayload(tokenDto: CreateTokenDto) {
      const a = {
          id: 15,
          uuid: '343443443444444444444444',
          userId: 36,
      };
      const token = this.tokenRepository.create(a);
      return await this.tokenRepository.save(token);
  }
  </code></pre>
  
  <p>But if I didn’t indicate the id of the token, I get an error.</p>
  
  <pre><code>private async savePayload(tokenDto: CreateTokenDto) {
      const a = {
          // id: 15,
          uuid: '343443443444444444444444',
          userId: 36,
      };
      const token = this.tokenRepository.create(a);
      return await this.tokenRepository.save(token);
  }
  </code></pre>
  
  <blockquote>
    <p>ER_DUP_ENTRY: Duplicate entry '36' for key 'REL_d417e5d35f2434afc4bd48cb4d'</p>
  </blockquote>
  
  <p>I searched and found some examples.</p>
  
  <p>1) <a href="https://stackoverflow.com/questions/46745688/typeorm-upsert-create-if-not-exist">TypeORM upsert - create if not exist</a></p>
  
  <p>2) <a href="https://github.com/typeorm/typeorm/issues/3342" rel="nofollow noreferrer">https://github.com/typeorm/typeorm/issues/3342</a></p>
  
  <p>They say that the value must be a primary key or a unique value. But my userId field is an index, and is also unique.</p>
  
  <p><a href="https://i.stack.imgur.com/2oZ8r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2oZ8r.png" alt="enter image description here"></a></p>
  
  <p>What options can be, why is my token not updated?</p>