gpt4 book ai didi

mysql - 在mysql中连接两个表时遇到SequelizeEagerLoadingError

转载 作者:行者123 更新时间:2023-11-29 09:53:33 26 4
gpt4 key购买 nike

当我在 mysql 中加入两个表时,出现以下错误消息。

老师

module.exports = function(sequelize, Sequelize) {
const Teachers = sequelize.define('Teachers', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
}
}, {
classMethods: {
associate: function (models) {
Teachers.hasMany(models.Students);
}
}
});
return Teachers;
}

学生

module.exports = function(sequelize, Sequelize) {
const Students = sequelize.define('Students', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
}
}, {
classMethods: {
associate: function (models) {
Students.belongsTo(models.Students);
}
}
});
return Students;
}

exports.allRegisteredStudents = function (teacherId) {
return models.Teachers.findAll({
where: {
id: teacherId
},
include: [models.Students]
});
};

请告诉我我错过添加的内容,谢谢。

最佳答案

学生不应该属于学生模型,学生应该属于教师模型应该是这样的:

module.exports = function(sequelize, Sequelize) {
const Students = sequelize.define('Students', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
}
}, {
classMethods: {
associate: function (models) {
Students.belongsTo(models.Teachers);
}
}
});
return Students;

}

exports.allRegisteredStudents = async function (teacherId) {
return await models.Teachers.findAll({
where: {
id: teacherId
},
include: [{ model: models.Students }]
});

};

关于mysql - 在mysql中连接两个表时遇到SequelizeEagerLoadingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54321736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com