Why am I not being able to set the SQL_SAFE_UPDATES with sequelize?

15 views Asked by At

I am trying to update a column of a table when running a seeder but I get the following error:

ERROR: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

My code is as follows:

await queryInterface.sequelize.transaction(async (transaction) => {
      await queryInterface.bulkDelete('Sales', null, { transaction });
      await queryInterface.bulkInsert('Sales', dummyJSON, { transaction });

      // Desactivar actualizaciones seguras
      await queryInterface.sequelize.query('SET SQL_SAFE_UPDATES = 0;', { transaction });

      // Actualizar la columna de ganancias
      await queryInterface.sequelize.query(`
        UPDATE Sales 
        SET profit = COALESCE(
          (SELECT SUM(Services.price) 
           FROM SalesServices 
           JOIN Services ON SalesServices.service_id = Services.id 
           WHERE SalesServices.sale_id = Sales.id), 0
        )
        WHERE id > 0;
      `, { transaction });

      // Re-activar actualizaciones seguras
      await queryInterface.sequelize.query('SET SQL_SAFE_UPDATES = 1;', { transaction });
    });

For some reason the SQL SAFE UPDATES lines are not being excecuted and if I try to do all of those lines in a single query like

        SET SQL_SAFE_UPDATES = 0;
        UPDATE Sales 
        SET profit = COALESCE(
            (SELECT SUM(Services.price) 
            FROM SalesServices 
            JOIN Services ON SalesServices.service_id = Services.id 
            WHERE SalesServices.sale_id = Sales.id), 0
        )
        WHERE id > 0;
        SET SQL_SAFE_UPDATES = 1;

It seems that sequelize is not supporting it.

Is there any other alternative to this?

Because if I exceute the query in my MySQL workbench, it works.

0

There are 0 answers