如何解决Sequelize急切加载错误:与用户无关的social_logins
这样定义的用户模型。
const db = require ('../../config/db_config');
const users = db.sequelize.define('users',{
id: {
type: db.DataTypes.INTEGER,primaryKey: true,autoIncrement: true,},first_name: {
type: db.DataTypes.STRING(150),last_name: {
type: db.DataTypes.STRING(150),email: {
type: db.DataTypes.STRING(256),required: true,unique: true
},password: {
type: db.DataTypes.STRING,student_id: {
type: db.DataTypes.STRING
},status: {
type: db.DataTypes.BOOLEAN,defaultValue: 0
},is_deleted: {
type: db.DataTypes.BOOLEAN,createdAt: db.DataTypes.DATE,updatedAt: db.DataTypes.DATE,});
module.exports = users;
social_logins模型定义如下
const db = require ('../../config/db_config');
const socialLogins = db.sequelize.define('social_logins',{
id: {
type: db.DataTypes.INTEGER,token: {
type: db.DataTypes.STRING
},tokenType: {
type: db.DataTypes.STRING
},fb_id: {
type: db.DataTypes.STRING
},user_id: {
type: db.DataTypes.INTEGER
},defaultValue: false
}
},{ underscored: true,timestamp: true,tableName: 'social_logins' });
module.exports = socialLogins;
使用belongsTo函数与social_logins模型关联的用户模型
socialLoginsModel.belongsTo(users)
续集抛出eagerLoadingError 错误EagerLoadingError [SequelizeEagerLoadingError]:social_logins是 与用户无关!
在运行下面给出的查询时。
const userModel = require ('./users_model');
const socialLoginModel = require('../social_logins/social_logins_model');
let id = "123456";
let email = "ex@example.com";
userModel.findOne({
where: { email },include: [{
model: socialLogins,where: {
fb_id: id
}
}]
});
解决方法
您应该基于您的型号名称拥有这样的关联
socialLogins.belongsTo(users)和users.hasOne(socialLogins)/ users.hasMany(socialLogins)基于您在DB中定义的关系
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。