美文网首页
typeorm upsert mysql/mariadb 联合

typeorm upsert mysql/mariadb 联合

作者: 此昵称已被狗抢占 | 来源:发表于2021-11-11 18:03 被阅读0次
    //.updateEntity(false) 的作用是避免 Error: Cannot update entity because entity id is not set in the entity.
    // https://github.com/typeorm/typeorm/issues/4651
    // https://stackoverflow.com/questions/66634526/typeorm-throws-an-error-after-insert-with-the-querybuilder-in-nest-js
    const qb = this.apiRepository
    .createQueryBuilder('p')
    .insert()
    .into(Api, [ 'name', 'url', 'method' ])
    .values([{name:'aaa',url:'/aaa','get'}])
    .updateEntity(false)
    .orUpdate({
      conflict_target: ['index__api', 'id'],
      overwrite: ['name']
    });
    
    const qb = this.apiRepository
    .createQueryBuilder('p')
    .insert()
    .into(Api, [ 'name', 'url', 'method' ])
    .values([{name:'bbb',url:'/aaa','get'}])
    .updateEntity(false)
    .orUpdate({
      conflict_target: ['index__api', 'id'],
      overwrite: ['name']
    });
    
    import {
      Entity,
      PrimaryGeneratedColumn,
      Column,
      ManyToOne,
      OneToMany,
      Tree,
      TreeChildren,
      TreeParent,
      JoinColumn,
      CreateDateColumn,
      Unique
    } from 'typeorm';
    
    @Entity()
    @Unique('index__api', ['url', 'method'])
    export class Api {
      @PrimaryGeneratedColumn({ name: 'id' })
      id: number;
    
      @Column({ name: 'name', nullable: false })
      name: string;
    
      @Column({ name: 'url', nullable: false })
      url: string;
    
      @Column({ name: 'method', nullable: false })
      method: string;
    
      @CreateDateColumn({ name: 'created_date', nullable: false })
      createdDate: Date;
    }
    
    

    相关文章

      网友评论

          本文标题:typeorm upsert mysql/mariadb 联合

          本文链接:https://www.haomeiwen.com/subject/zejgzltx.html