类似于:
How to insert an entity with OneToMany relation in NestJS?
我有两个实体(书本和学生):
@Entity({ name: 'book' })
export class Book {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'boolean', default: false })
isRead: boolean;
@OneToOne(() => Student, assignedStudent=> assignedStudent.assignedBooks, { nullable: true })
assignedStudent: Student;
}
和
@Entity({ name: 'student' })
export class Student {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToMany(type => Book, assignedBooks => assignedBooks.assignedStudent, { cascade: ['insert', 'update'], nullable: true })
assignedBooks: Book[];
}
每个学生都读书,一个学生可以读一本书,一旦读书就被标记为这样(isRead = true)。一个学生一次只能读一本书(社区学院...),并且只能在完成当前一本书之后才开始阅读下一本书(学生和书籍可以彼此独立存在;关系(“阅读”)是稍后建立。
我尝试过的事情:
// Get all students
const currentStudent = await getConnection().
getRepository(Student).
createQueryBuilder().
leftJoinAndSelect("agent.assignedBooks", "assignedBooks").getMany();
// Iterate through books list; if all are read, add a new book (and
// save both the book, student and update relationship)
如何更新关系数组(在一个查询中)?这实际上是不是作业。