gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:30 28 4
gpt4 key购买 nike

我正在研究"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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com