I have the following typescript TypeORM model:
@Entity()
export class Teacher extends EntityBase {
@Column({ length: 256 })
public firstName: string
@Column({ length: 256 })
public lastName: string
@Column({ unique: true, length: 256 })
public email: string
@ManyToMany((type) => Student, (student) => student.teachers, { nullable: true })
@JoinTable()
public students: Student[]
constructor(first: string, last: string, email: string) {
super();
this.firstName = first;
this.lastName = last;
this.email = email;
//this.students = []; TypeORM initialization failed! InitializedRelationError: Array initializations are not allowed in entity relations
}
}
The following query:
public override async GetByEmail (email: string): Promise<Teacher | null> {
return await this._repository.findOneOrFail({
where: { email: email },
relations: ["students"]
});
}
failed:
Exception: {"criteria":{"where":{"email":"teacherken@gmail.com"},"relations":["students"]},"message":"Could not find any entity of type \"Teacher\" matching: {\n \"where\": {\n \"email\": \"teacherken@gmail.com\"\n },\n \"relations\": [\n \"students\"\n ]\n}"}
findOneOrFail
will throw. Need to catch exception and handle it as non-existing entity. I switched to findOne