Sequelize设置相关值

时间:2017-08-31 21:51:25

标签: node.js associations sequelize.js

我有两个模型,员工 Office 。每个员工都属于一个办公室,办公室有很多员工。

我很难弄清楚如何使用Sequelize更新员工的办公室。

员工模型如下:

let Employee = sequelize.define("Employee", {
    id: {
        field: 'id',
        type: DataTypes.INTEGER,
        primaryKey: true,
        allowNull: false,
        autoIncrement: false
   },
   name: {
        field: 'name',
        type: DataTypes.STRING(255),
        allowNull: false
   }
}, {
    freezeTableName: true,
    timestamps: false,
    deletedAt: false
})

Employee.associate = models => {
    Employee.belongsTo(models.Office, {
        foreignKey: 'id'
    })
}

Office 模型如下:

let Office = sequelize.define("Office", {
    id: {
        field: 'id',
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
   },
   name: {
        field: 'name',
        type: DataTypes.STRING(255),
        allowNull: false
   }
}, {
    freezeTableName: true,
    tableName: 'Lkup_Office',
    timestamps: false,
    deletedAt: false
})
Office.associate = models => {
    Office.hasMany( models.Employee, {
        foreignKey: 'id'
    })
}

在数据库中,我有以下员工:

{
    "id": "2",
    "name" : "John",
    "office": {
        "id": 2,
        "name" : "Seattle"
    }
}

......以及以下办事处:

[
    {
        "id": 1,
        "name" : "Chicago"
    },
    {
        "id": 2,
        "name" : "Seattle"
    }
]

我想将员工(1)办公室的ID从2'Seattle')更改为1'Chicago');问题是使用以下查询

// PUT /2/office/1

router.put('/:employeeId/office/:officeId', (req, res) => {
    models.Employee.findOne({
        where:{id:req.params.employeeId},
        include:[{model:models.Office}]
    }).then( employee => {

        models.Office.findOne({
            where:{id:req.params.officeId},
            include:[{model:models.Employee}]
        }).then( office => {

            employee.setOffice( office ).then( result => {
                res.send( result )
            })

        })
    })
})

......我的员工办公室没有更新:

{
    "id": "2",
    "name" : "John"
    "office": {
        "id": 2
        "name" : "Seattle"
    }
}

事实上,它根本没有做任何事情:没有错误,数据库也没有改变。我怀疑我有什么不正确的事情,但Sequelize并没有抛出任何错误。

1 个答案:

答案 0 :(得分:0)

router.put('/:employeeId/office/:officeId', (req, res) => {

    /*In this first part, I just separated the two queries into two
      separate variables*/

    let employeeInfo = models.Employee.findOne({
        where: {
            id: req.params.employeeId
        },
        include: [{
            model: models.Office
        }]
    });

    let officeInfo = models.Office.findOne({
        where: {
            id: req.params.officeId
        },
        include: [{
            model: models.Employee
        }]
    });

    /*In this next part, we have to use Promise.all, which comes with 
      bluebird, which is already a part of Sequelize.js. How this next
      part is executed will depend on how you have your Sequelize.js code
      structured in your application. This may require some trial and error*/

    models.sequelize.Promise.all([employeeInfo, officeInfo]).then(([Employee, Office])=> {
        return Employee.setOffice(Office);
    }).catch(err => {
        console.log(err);
    });
});