Home > database >  How do i add data into a join table using TypeOrm?
How do i add data into a join table using TypeOrm?

Time:09-19

This is the Entity

i want to insert data into this table through an api, it contains foreign key references like student_id and roll_id.

i commented out student_id and roll_id, so as to avoid multiple keys with the same name.

Please help me out :)

CodePudding user response:

I believe you're just looking for how to insert?

async function saveStudentRollState(data: StudentRollState) {
  const repo = getRepository(StudentRollState);
  const savedRecord = await repo.save(data);
  return savedRecord;
}

CodePudding user response:

try this for Entity definition.

@Entity()
export class StudentRollState {
  ...
  @Column()
  student_id: number;
  
  @Column()
  roll_id: number;
  
  @ManyToOne(()=>Student, (student) => student.id)
  student: Student;
  
  @ManyToOne(()=>Roll, (roll) => roll.id)
  roll: Roll;
}

And now if you insert data your duplicating should go away.

  • Related