我正在尝试通过联结表插入与其他2个实体关联的新记录。像这样:
员工 - < deploymentCrew> - 部署 - < deploymentEquipment> - 设备
员工和部署表:
module.exports = function(sequelize, Sequelize) {
var Staff = sequelize.define('staff', {
_id: {
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
_firstname: {
type: Sequelize.STRING,
notEmpty: true
},
_lastname: {
type: Sequelize.STRING
},
_phonework: {
type: Sequelize.STRING
},
_phonehome: {
type: Sequelize.STRING
},
_img: {
type: Sequelize.STRING
},
_emailpersonal: {
type: Sequelize.STRING
},
_emailwork: {
type: Sequelize.STRING
},
_position: {
type: Sequelize.STRING
},
_notes: {
type: Sequelize.STRING
},
_status: {
type: Sequelize.STRING,
defaultValue: 'active'
}
});
return Staff;
};
module.exports = function(sequelize, Sequelize) {
var Deployment = sequelize.define('deployment', {
_id: {
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
_datedeployed: {
type: Sequelize.DATEONLY,
notEmpty: true
},
_datereturned: {
type: Sequelize.DATEONLY
},
_city: {
type: Sequelize.STRING
},
_province: {
type: Sequelize.STRING
},
_country: {
type: Sequelize.STRING
},
_unitnumber: {
type: Sequelize.STRING
},
_comments: {
type: Sequelize.TEXT
},
_productid: {
type: Sequelize.INTEGER
},
_contractid: {
type: Sequelize.INTEGER
},
_deploymenttypeid: {
type: Sequelize.INTEGER
},
_finalsold: {
type: Sequelize.NUMERIC(17,3)
},
_pricing: {
type: Sequelize.TEXT,
get: function () {
return JSON.parse(this.getDataValue('value'));
},
set: function (value) {
this.setDataValue('value', JSON.stringify(value));
}
},
_status: {
type: Sequelize.STRING,
defaultValue: 'active'
},
createdBy: {
type: Sequelize.STRING
}
});
return Deployment;
};
我的联结表代码就像这样:
module.exports = function(sequelize, Sequelize) {
var DeploymentEquipment = sequelize.define('deploymentEquipment', {
});
DeploymentEquipment.associate = function(models){
models.equipment.belongsToMany(models.deployment, {through: models.deploymentEquipment, foreignKey: 'equipmentId'});
models.deployment.belongsToMany(models.equipment, {through: models.deploymentEquipment, foreignKey: 'deploymentId'});
};
return DeploymentEquipment;
};
module.exports = function(sequelize, Sequelize) {
var DeploymentCrew = sequelize.define('deploymentCrew', {
_timex: {
type: Sequelize.DATEONLY,
notEmpty: true
},
_dateon: {
type: Sequelize.DATEONLY
},
_dateoff: {
type: Sequelize.DATEONLY
},
_role: {
type: Sequelize.STRING
}
});
DeploymentCrew.associate = function(models){
models.staff.belongsToMany(models.deployment, {through: models.deploymentCrew, foreignKey: 'staffId'});
models.deployment.belongsToMany(models.staff, {through: models.deploymentCrew, foreignKey: 'deploymentId'});
};
return DeploymentCrew;
};
这一切似乎都很有效。但是我的create语句在insert语句中省略了deploymentid。这是我的代码:
var deployment = req.body;
var crew = req.body._deploymentcrew;
var equipment = req.body._deploymentequipment;
//insert new deployment - start transaction, add deployment, get ID, loop through crew, loop through equipment
models.sequelize.transaction(t =>{
var promises = [];
return models.deployment.create(req.body, {transaction: t}).then(function(newDeployment) {
for(var i=0; i < equipment.length; i++){
var equip = equipment[i];
equip.deploymentid = newDeployment.id;
equip.equipmentId = equipment[i].id;
var newPromise = models.deploymentEquipment.create(equip, promises.push(newPromise));
}
for(var i=0; i < crew.length; i++){
var staff = crew[i];
staff.deploymentid = newDeployment.id;
staff.staffId = crew[i].staff.id;
var newPromise = models.deploymentCrew.create(staff, promises.push(newPromise));
}
return Promise.all(promises)
});
当我查看控制台时,这是插入语句:
执行(默认):INSERT INTO deploymentCrews
(_timex
,_dateon
,_dateoff
,_role
,createdAt
,{{1} },updatedAt
)价值观(&#39; 2018-03-01&#39;,&#39; 2018-03-12&#39;,N
ULL,&#39; lead&#39;,2018-03-02 19:21:10&#39;,&#39; 2018-03-02 19:21:10&#39;,5);
请注意,有staffId,但没有deploymentId。有什么想法? 我尝试过使用include:[models._deploymentcrew],但我不确定这种方式可以处理事务。
任何建议都将非常感谢。谢谢!
答案 0 :(得分:0)
在创建N:M关联时,我们需要指定两个表的外键,如下所示
Staff.associate = function(models){
Staff.belongsToMany(models.deployment, {through: models.deploymentCrew, foreignKey: 'staffId'});
};
Deployment.associate = function(models) {
Deployment.belongsToMany(models.staff, {through: models.deploymentCrew, foreignKey: 'deploymentId'});
};
将上述内容添加到相应的表定义中,并删除deploymentCrew中的关联