Sequelize - 未处理的拒绝错误未关联

Sequelize - Unhandled Rejection Error Not Associated

在 运行 查询后,我收到了一个奇怪的关联错误,其中 table 连接与被查询的主要 table 具有一对多关系。尽管在我的数据库中设置了关联并在我的模型中显示了关系,但我仍然收到错误。我错过了什么可能导致这个?请注意,请求 table 是使用 cli 使用迁移文件创建的。这会是关键问题吗?

这里是错误:

Unhandled rejection Error: user is not associated to request!

这是我的查询:

models.Request.findAll({
                include: [{
                    model: models.User
                }],
                where: {
                    $or: [{requester: req.user.userId}, {receiver: req.user.userId}]
                }
            });

这是用户 table,其中存在一对多连接:

module.exports = function(sequelize, DataTypes) {

var User = sequelize.define('user', {
    userId: {
        type: DataTypes.INTEGER,
        field:'user_id',
        autoIncrement: true,
        primaryKey: true
    },
    firstName: {
        type: DataTypes.STRING,
        field: 'first_name'
    },
    lastName: {
        type: DataTypes.STRING,
        field: 'last_name'
    },
    email: {
        type: DataTypes.STRING,
        isEmail: true,
        unique: true,
        set: function(val) {
            this.setDataValue('email', val.toLowerCase());
        }
    },
    password: DataTypes.STRING,
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        allowNull: true
    },
    teamId: {
        type: DataTypes.INTEGER,
        field: 'team_id',
        allowNull: true
    },
}, {
    underscored: true,
    freezeTableName: true,
    classMethods: {
        associate: function(db) {
            User.hasMany(db.Request, { foreignKey: 'requester'});
        },  
});
    return User;
}

这里是请求 table(这个table最初是用cli迁移的):

module.exports = function(sequelize, DataTypes) {

var path = require('path');
var moment = require('moment');
var current = new Date();
var day = ("0" + current.getDate()).slice(-2);
var month = ("0" + (current.getMonth() + 1)).slice(-2);
var today = current.getFullYear() + '-' + (month) + '-' + (day); 


var Request = sequelize.define('request', {
    requestId: {
        type: DataTypes.INTEGER,
        field: 'request_id',
        autoIncrement: true,
        primaryKey: true
    },
    requestDate: {
        type: DataTypes.DATE,
        field: 'request_date',
        isDate: true,
        allowNull: false,
        defaultValue: today
    },
    requester: {
        type: DataTypes.INTEGER,
        field: 'requester',
        references: {
            model: 'user',
            key: 'user_id'
        },
        onUpdate: 'cascade',
        onDelete: 'cascade'
    },
    receiver: {
        type: DataTypes.INTEGER,
        field: 'receiver'
    },
},
 {
    underscored: true,
    freezeTableName: true,
});
    return Request;
}

db-index(创建关联的地方):

    var Sequelize = require('sequelize');
    var path = require('path');
    var sequelize = new Sequelize(process.env.LOCAL_DATABASE || process.env.RDS_DATABASE, 
            process.env.LOCAL_USERNAME || process.env.RDS_USERNAME, 
            process.env.LOCAL_PASSWORD || process.env.RDS_PASSWORD, {
        host: process.env.RDS_HOSTNAME || 'localhost',
        port: process.env.RDS_PORT || '3306',
        dialect: 'mysql',
        timezone: 'America/New_York'
    });

    sequelize.authenticate().then(function(err) {
        if (!!err) {
            console.log('Unable to connect to the database:', err)
        } else {
            console.log('Connection has been established successfully.')
        }
    });

    var db = {}

    db.User = sequelize.import(__dirname + "/user");
    db.Request = sequelize.import(__dirname + "/request");

    db.User.associate(db);

    db.sequelize = sequelize;
    db.Sequelize = Sequelize;

    sequelize.sync();

module.exports = db;

请求关系截图table:

为了使用 Request 模型获得 User,您应该定义关联 Request->belongsTo->User。存在关系 User->hasMany->Request 告诉我们可以通过 User 模型得到 Requests,而不是反向