首页 文章

Sequelize Model.update with include where

提问于
浏览
2

我想根据JobCard Model状态设置Cars模型的状态

Cars.update({
            status: 'done'
        },
        {
            where: {
                status: 'pending'
            },
        },
        {
            include: [
                {
                    model: sequelize.models.JobCard,
                    where: {
                        id: jobcard_id
                    }
                }
            ]
        })
        .then((result) => {
            console.log(result);
        });

但是在日志中我只看到了这个查询

UPDATE "Cars" SET "status"='done',"updatedAt"='2017-10-22 15:57:24.854 +00:00' WHERE "status" = 'pending';

我错过了什么吗?

1 回答

  • 0

    include 属性应该是options对象的一部分(第二个参数传递给指定了 where.update() 方法),例如:

    Cars.update({
            status: 'done'
        },
        {
            where: {
                status: 'pending'
            },
            include: [
                {
                    model: sequelize.models.JobCard,
                    where: {
                        id: jobcard_id
                    }
                }
            ]
        }
    

    但是,Sequelize并不(据我所知)支持使用 include 作为传递给 .update() 方法的options参数的一部分 . 每次我尝试这样做,即使刚才(在Sequelize v4.42.0中),也会抛出错误并且查询未成功完成 .

相关问题