I have this entity class. It's an association between two tables.
import { Column, Entity, Index, JoinColumn, ManyToOne } from "typeorm";
import { Classroom } from "./Classroom";
import { Teacher } from "./Teacher";
@Index("fk_class_has_teacher_teacher1_idx", ["teacherId"], {})
@Index("fk_class_has_teacher_class1_idx", ["classId"], {})
@Entity("teacherclassroom", { schema: "school" })
export class Teacherclassroom {
@Column("int", { primary: true, name: "class_id" })
classId: number;
@Column("int", { primary: true, name: "teacher_id" })
teacherId: number;
@Column("datetime", {
name: "reg_date",
nullable: true,
default: () => "CURRENT_TIMESTAMP"
})
regDate: Date | null;
@ManyToOne(
() => Classroom,
classroom => classroom.teacherclassrooms,
{ onDelete: "NO ACTION", onUpdate: "NO ACTION" }
)
@JoinColumn([{ name: "class_id", referencedColumnName: "id" }])
class: Classroom;
@ManyToOne(
() => Teacher,
teacher => teacher.teacherclassrooms,
{ onDelete: "NO ACTION", onUpdate: "NO ACTION" }
)
@JoinColumn([{ name: "teacher_id", referencedColumnName: "id" }])
teacher: Teacher;
}
What I'm trying to do is get Teacher and Classroom details when I'm retrieving a row from this association. I did it as follows,
import "reflect-metadata";
import { createConnection, getRepository } from "typeorm";
import { Teacherclassroom } from "./entity/Teacherclassroom";
import { Classroom } from "./entity/Classroom";
import { Teacher } from "./entity/Teacher";
createConnection().then(async connection => {
const myRepository = getRepository(Teacherclassroom);
const test = myRepository.find({ relations: ["teacher", "classroom"] });
console.log(test);
}).catch(error => console.log(error));
I'm getting this error,
Relation was not found, please check if it is correct and really exist in your entity.
Just Change class
to classroom
in Classroom relation
@ManyToOne(() => Classroom,classroom => classroom.teacherclassrooms,
{ onDelete: "NO ACTION", onUpdate: "NO ACTION" })
@JoinColumn([{ name: "class_id", referencedColumnName: "id" }])
classroom: Classroom;