Sequelize workFlow与用户无关

问题描述 投票:1回答:1

我正在研究“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)

node.js associations sequelize.js has-many belongs-to
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.