我正在研究"续集":"^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);
下面给出的 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;
}
工作流模式.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;
});
我收到波纹管错误
{ SequelizeEagerLoadError: workFlow 与用户无关! at Function._getIncludedAssociation (ode_modules\sequelize\lib\model.js:582:13)
从提供的代码来看,关联可能在执行之前未正确同步。通常,如 Sequelize 文档中所示,最佳做法是在定义模型的文件中关联模型。
如果无法做到这一点,请确保更改与数据库同步。