Skip to content
Advertisement

Sequelize – is it possible to limit the number of record in junction table

There are 3 tables student_teacher, student, teacher with below relationship.

Each teacher will be responsible for 5 students, so the relationship should be 1 to Many, but I decide to create a junction table for storing extra information for this relationship.

When I create a student_teacher record, the payload will be like this:

Let’s say I have record below now in table student_teacher:

There are already 5 record for teacherA in table student_teacher, I will to forbid to create 1 more record for teacherA.

Is it possible to do it in Sequelize? Or handle I need to handle it in node.js function?

student-teacher.model.js

student.model.js

teacher.model.js

Advertisement

Answer

Sequelize’s hooks are very compatible with your requirement:

Read more: https://sequelize.org/master/manual/hooks.html

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement