typescript - 如何查询和改变表中的“多对多”字段?
问题描述
我是nestjs的新手。我有两个实体:
@Entity({ name: "Books" })
@ObjectType()
export class Book {
@PrimaryGeneratedColumn()
@Field()
id: number;
@Column()
@Field()
title: string;
@ManyToMany(() => Author, (author: Author) => author.books)
@Field(() => [Author])
authors: Author[];
}
@Entity({ name: "Authors" })
@ObjectType()
export class Author {
@PrimaryGeneratedColumn()
@Field()
id: Number;
@Column()
@Field()
firstName: string;
@Column()
@Field()
lastName: string;
@ManyToMany(() => Book, (book: Book) => book.authors)
@JoinTable()
@Field(() => [Book])
books: Book[];
}
这会自动给我第三个空表“authors_books_books”。如何建立作者与书籍的关系?不仅在摘要作者和摘要书之间,而且还指定这个具有 id:x 的作者写了具有 id:y? 的书。换句话说:如何用数据填充“authors_books_books”表?
解决方案
Typeorm 在技术上确实有内置的方法来处理这个......
也就是说,我更喜欢不需要对象的快速方法:
await this.manager.query(
'INSERT INTO authors_books_books ("authorId", "bookId") VALUES ($1, $2)',
[authorId, bookId],
);
推荐阅读
- python - 使用给定包的所有 PyPI 包
- javascript - Firebase Javascript 多次删除单击
- android - 如何在 Android 中制作 Top Dialog 或 sheet?
- android - Android:XML文件的设计视图是白色和空白
- facebook - 通过 Graph API - OpenGraphObjectRestrictions 获取 facebook 页面限制(国家、年龄)
- javascript - javascript 对象的性能相关问题
- javascript - 用户可以利用 eval() 发送 xmlHttpRequest 并访问我的服务器吗?
- java - Hibernate OneToOne 两个嵌入 Id 之间的双向关系
- spring-boot - 如何将WebsocketSession存储在redis缓存中?
- c# - 如何在字典 C# 中使用元组作为键