//.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;
}
网友评论