I have three tables/models in sequelize, Teachers, Classes, and Divisions. I have established many to many relationship between Teachers and Divisions. and One to Many relationship between Classes and Divisions.
I am fetching data from database like this:
const data = await TProfile.findAll({
where: {},
include: [
{
model: User,
attributes: ["uuid", "name", "email", "mobile"],
},
{
model: Division,
include: Klass,
attributes: ["classId", "divisionId", "divisionName"],
},
],
});
Now, I want to filter data based on classId and divisionId. Something like this:
const data = await TProfile.findAll({
where: {},
include: [
{
model: User,
attributes: ["uuid", "name", "email", "mobile"],
},
{
model: Division,
where: { divisionId }
include: {
model: Klass,
where: { classId }
},
attributes: ["classId", "divisionId", "divisionName"],
},
],
});
divisionId and classId both are optional.
When I am trying to filter with divisionId i.e. where: { divisionId }
, I am getting error Unknown column 'Teacher.divisionId' in 'where clause'
, and when doing same with klassid i.e. where: { classId }
getting same error Unknown column 'Teacher.classId' in 'where clause'
Is there anyother way to do same like this?
When I am fetching through Division, I am able to filter based on divisionId
though I am not getting results as structured when fetching through Teacher model and not able to found a way to get teachers associated with class.
I also tried using approach where I can fetch Classes, Divisions and then Teachers but that’s also not working.
Is there any suggestion to do this better?