Sequelize workFlow与用户无关

时间:2018-03-05 11:53:01

标签: node.js associations sequelize.js has-many belongs-to

我正致力于" sequelize":" ^ 4.32.2" &当我使用belongsTO协会时面临问题。以下是我的代码

这是我在主文件中创建表之间的关联。

var Sequelize = require('sequelize');
var sequelize = require('./../database')(Sequelize);
var Users = sequelize.import('./usersSchema');
var WorkFlows = sequelize.import('./workFlowsSchema');

//Create user association with workFLow
Users.hasMany(WorkFlows);
//Create workFlow association with user
WorkFlows.belongsTo(Users);

usersSchema.Js文件代码如下所示

module.exports = function(sequelize, DataTypes) {
//create table schema
var Users = sequelize.define('user', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    email: {
        type: DataTypes.STRING
    }
}, {
    timestamps: true, // timestamps will now be true,
});
//bydefault force is false if it's true then it delete table & create it again

Users.sync({
    force: false
});
return Users;

}

workFlowsSchema.js文件包含代码

module.exports = function(sequelize, DataTypes) {
//create table schema
var WorkFlows = sequelize.define('workFlow', {
    assign_to: {
        type: DataTypes.STRING
    },
    assign_by: {
        type: DataTypes.STRING
    },
    userId: {
        type: DataTypes.INTEGER,
    },
    status_id: {
        type: DataTypes.INTEGER
    },
    hash_id: {
        type: DataTypes.INTEGER
    },
    pair_code: {
        type: DataTypes.INTEGER
    },
    archived: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
    },
    start_time: {
        type: DataTypes.DATE,
    },
    end_time: {
        type: DataTypes.DATE,
    },
}, {
    timestamps: true, // timestamps will now be true
});


WorkFlows.sync({
    force: false // timestamps will now be true
});

return WorkFlows;

}

执行时

var Sequelize = require('sequelize');
var sequelize = require('./../database')(Sequelize);
var Users = sequelize.import('./usersSchema');
var workFlows = sequelize.import('./workFlowsSchema');
const Op = Sequelize.Op;

Users.count({
    where: conditions,
    include: [{
        all:false,
        model: workFlows
       }]
}).then(function(count) {
    return count;
}).catch(function(err) {
    return err;
});

我得到了吼叫错误

{SequelizeEagerLoadingError:workFlow与用户无关!     在Function._getIncludedAssociation(\ node_modules \ sequelize \ lib \ model.js:582:13)

1 个答案:

答案 0 :(得分:0)

从提供的代码中,很可能在执行之前未正确同步关联。通常,如Sequelize文档中所示,最佳做法是将模型与定义它们的文件相关联。

如果无法做到这一点,请确保更改与数据库同步。