node.js - Sequelize工作流程不与用户关联

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

我正在研究"sequelize": "^4.32.2"并且在使用belongTO关联时遇到问题。下面是我的代码

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

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);

userSchema.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:工作流程未与用户关联! 在 Function._getIncludedAssociation (\node_modules\sequelize\lib\model.js:582:13)

最佳答案

从提供的代码来看,关联可能在执行前未正确同步。一般来说,如 Sequelize 文档中所示,最佳实践是在定义模型的文件中关联模型。

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

关于node.js - Sequelize工作流程不与用户关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49109927/

相关文章:

node.js - 在socket.io中获取消息发送者的客户端ID?

Sequelize 的 Mysql 连接超时

node.js - 相当于nodeJS Express中的rails控制台

c# - 将二进制 32 位 IEEE-754 浮点转换为 .net 中的 Double

php - 如何使用 TripleDES 加密与 Node.js 和 PHP-mcrypt 获得相同的结果?

node.js - 有没有办法在 Sequelize 中将一个表的一个属性与另一个表的任何属性相关联?

ruby-on-rails - 使用vestal_versions的版本关联?

ruby-on-rails - 在 Ruby on Rails 中的多对多关系中,如何使用父表表单中的数据在连接表中创建记录?

javascript - 有没有办法使用 Sequelize 模型方法来表达以下 SQL 查询?

javascript - Order By (alias) then Order by second sequelize