'Sequelize Model.update with include where

I want to set the status of Cars model to done based on JobCard Model status

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

But in the logs I am seeing this query only

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

Am I missing anything?



Solution 1:[1]

The include attribute should be part of the options object (the second parameter passed to the .update() method where you have where specified), for example:

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

However, Sequelize does not (to my knowledge) support the use of include as part of the options parameter passed to the .update() method. Each time I've ever tried to do so, even just now (in Sequelize v4.42.0), an error is thrown and the query is not completed successfully.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Head