Rahul Kumawat
Rahul Kumawat

Reputation: 195

how to update records in association though sequelize in node js?

we are trying to update records in association though sequelize but its only updating the records of model on which it is fired or direct model no associated model affected by the query .

any pathway to perform this

return db.product.findById( 75759,{
                include: [
                    {
                        model: db.productDescription
                    }
                ]})
            .then(product => {
                var rahulpro = {};
                Object.assign(rahulpro,product);
                // console.log(rahulpro.product_description.description);
                rahulpro.name = "44444";
                rahulpro.product_description.dataValues.description = "rahul";
                product.updateAttributes(rahulpro);
              });

What do you expect to happen?

we want to update data of description model

What is actually happening?

Nothing changed database of description

Database version: mysql 5.7 Sequelize version: 4.8.3

Upvotes: 0

Views: 526

Answers (1)

DillGromble
DillGromble

Reputation: 383

I've had a very similar issue using Sequelize. After hours of looking into it, I've seen there is no great way to handle data attached to through tables.

For what you're trying to do, you'll have to perform 2 queries, find the product instance from your product table, and then query the product_description table where description.product_id === product.id.

Then write the changes to the description instance, then to the product instance.

This was a shortcoming of ORM's in my project, and for more complex queries we started using raw sequel because it gave us the ability to do things like this.

Upvotes: 2

Related Questions