我正在尝试通过连接表插入与 2 个其他实体关联的新记录。像这样:
人员-<部署人员>-部署-<部署设备>-设备
人员和部署表:
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
, updatedAt
, staffId
) VALUES ('2018-03-01','N-201ULL,'铅','2018-03-02 19:21:10','2018-03-02 19:21:10',5);
请注意,有staffId,但没有deploymentId。任何想法为什么?
我曾尝试使用 include:[models._deploymentcrew] 但我不确定用这种方式处理事务。
任何建议将不胜感激。谢谢!
最佳答案
在创建 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 中的关联
关于node.js - Sequelize 使用包含和连接表创建事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49076039/