【发布时间】:2020-08-29 19:34:23
【问题描述】:
我有一个问题,如何使用 TypeORM 多对多关系保存在 NestJs 中。 我尝试推送只会响应,不会保存在 cat 和 book 的 joinTable id 中。 谢谢!
service.ts
async addBookToCat(catId: string, bookId: string): Promise<any> {
const cat = await this.catRepository.findOne(catId);
if(!cat) {
throw new NotFoundException('Category not found!');
}
const book = await this.bookRepository.findOne(bookId);
if(!book) {
throw new NotFoundException('Book not found!');
}
cat.book.push(book);
return cat;
}
Category.entity.ts
@ManyToMany(type => Book, book => book.cat, {eager: true})
@JoinTable()
book: Book[];
Book.entity.ts
@ManyToMany(type => Category, cat => cat.book, {eager: false, cascade: true})
cat: Category[];
【问题讨论】:
标签: typescript many-to-many nestjs typeorm