У меня есть 2 модели, которые они дали: м отношение: учитель, класс
Теперь я хочу сделать запрос, чтобы показать текущие классы учащихся, за которые они отвечают (не все классы в таблице).
Это моя модель класса:
module.exports = (sequelize, DataTypes) => {
var Model = sequelize.define('Class', {
name : DataTypes.STRING,
category : {type: DataTypes.STRING, unique: true},
day : DataTypes.ENUM('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'),
hour : DataTypes.STRING,
});
Model.associate = function(models){
this.Students = this.belongsToMany(models.Student, {through: 'StudentClass'});
};
Model.associate = function(models){
this.Staffs = this.belongsToMany(models.Staff, {through: 'ClassTeacher'});
};
Model.prototype.toWeb = function (pw) {
let json = this.toJSON();
return json;
};
return Model;
Модель учителя:
module.exports = (sequelize, DataTypes) => {
var Model = sequelize.define('Staff', {
first : DataTypes.STRING,
last : DataTypes.STRING,
email : {type: DataTypes.STRING, allowNull: true, unique: true, validate: { isEmail: {msg: "Email invalid."} }},
phone : {type: DataTypes.STRING, allowNull: true, unique: true, validate: { len: {args: [7, 20], msg: "Phone number invalid, too short."}, isNumeric: { msg: "not a valid phone number."} }},
password : DataTypes.STRING,
role : DataTypes.ENUM('Head', 'Teacher'),
});
Model.associate = function(models){
this.Students = this.belongsToMany(models.Student, {through: 'TeacherStudent'});
};
Model.associate = function(models){
this.Classes = this.belongsToMany(models.Class, {through: 'TeacherClass'});
};
Что я нашел и попробовал:
Class.findAll({
attributes: ['id', 'name'],
include: [{
model:Staff,
attributes: ['first', 'last'],
through: {
attributes: ['StaffId', 'ClassId'],
// where:{ClassId:1}
}
}]
}).then(classes => {
res.send(classes);
});
Но это показывает мне все классы.
Заранее спасибо за помощь.