代码之家  ›  专栏  ›  技术社区  ›  enfrost

使用include和junction表继续创建事务

  •  0
  • enfrost  · 技术社区  · 8 年前

    我试图插入一条新记录,该记录通过连接表与其他两个实体关联。这样地:

    员工-<deploymentCrew>-部署-<部署设备>-设备

    人员和部署表:

    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语句:

    执行(默认):插入到 deploymentCrews ( _timex , _dateon , _dateoff , _role , createdAt , updatedAt , staffId )数值('2018-03-01','2018-03-12',N ULL,'lead','2018-03-02 19:21:10','2018-03-02 19:21:10',5);

    请注意,这里有staffId,但没有deploymentId。有什么想法吗?为什么? 我尝试过使用include:[models.\u deploymentcrew],但我不确定如何处理事务。

    如有任何建议,将不胜感激。谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   Bharathvaj Ganesan    8 年前

    在创建N:M关联时,我们需要 指定两个表的ForeignKey 如下所示

    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中的关联

    推荐文章