Monday 24 February 2020

Multiple updates to MySQL tables using Sequelize?

Hello to all! Looking for help for anyone that might be familiar with Sequelize.I'm trying to resolve a problem between two tables where it's a One To Many relationship. Basically once many customers have been created, I need to be able to save each customer to one booth. Each booth has many customer_ids, and each customer has one booth_id.My problem is my new Booth instance doesn't create a new booth_id which means I can't update my customer_id for each customer. Kinda stuck on where to go from here.Below is my post statement!// Post new customer app.post("/customers", (req, res) => { const { tableData } = req.body; db.Customers.bulkCreate(tableData) .then(customers => { const newTable = db.Booths.build(); customers.forEach(customer => { customer.update({ booth_id: newTable.booth_id, active: "true" }); }); newTable.save(); }) .catch(err => { console.log(err.message); }); }); This is what gets log in the console Executing (default): INSERT INTO `Customers` (`customer_id`,`customer_name`,`customer_phone`,`active`) VALUES (NULL,'customer1','1111111111','false'),(NULL,'customer2','2222222222','false'); [0] Executing (default): INSERT INTO `Booths` (`booth_id`,`active`) VALUES (DEFAULT,?); [0] Executing (default): UPDATE `Customers` SET `booth_id`=?,`active`=? WHERE `customer_id` = ? [0] Executing (default): UPDATE `Customers` SET `booth_id`=?,`active`=? WHERE `customer_id` = ? Any ideas?

Submitted February 24, 2020 at 11:42PM by kingducasse

No comments:

Post a Comment