JozeRi
JozeRi

Reputation: 3439

Running a migration on Sequelize is throwing a deadlock error

So, my project has a MySQL database set up using Sequelize, backend is written in nodeJS. I'm trying to perform a simple migration to my database, adding a new column to 4 different tables.

When I'm running my migration, I get the following error:

== 20210419115251-add-duplication-of-column-feature: migrating =======

ERROR: Deadlock found when trying to get lock; try restarting transaction

This is my code:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return Promise.all([
      queryInterface.addColumn('Forms', 'duplicationOf', {
        type: Sequelize.INTEGER,
        after: 'state'
      }),
      queryInterface.addColumn('FormFields', 'duplicationOf', {
        type: Sequelize.INTEGER,
        after: 'reconcileType'
      }),
      queryInterface.addColumn('FormFieldsOptions', 'duplicationOf', {
        type: Sequelize.INTEGER,
        after: 'height'
      }),
      queryInterface.addColumn('FormFieldsLogics', 'duplicationOf', {
        type: Sequelize.INTEGER,
        after: 'data'
      })
    ]);
  },

  down: (queryInterface, Sequelize) => {
    return Promise.all([
      queryInterface.removeColumn('Forms', 'duplicationOf'),
      queryInterface.removeColumn('FormFields', 'duplicationOf'),
      queryInterface.removeColumn('FormFieldsOptions', 'duplicationOf'),
      queryInterface.removeColumn('FormFieldsLogics', 'duplicationOf')
    ]);
  }
};

What am I missing? Thanks a lot in advance

Upvotes: 2

Views: 2509

Answers (1)

Erez Hod
Erez Hod

Reputation: 1883

Try wrapping it with a Sequelize transaction to make sure everything executes successfully like so:

You can find it on their documentation page here.

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(t => {
      return Promise.all([
        queryInterface.addColumn('Forms', 'duplicationOf', {
          type: Sequelize.INTEGER,
          after: 'state'
        }, { transaction: t }),
        queryInterface.addColumn('FormFields', 'duplicationOf', {
          type: Sequelize.INTEGER,
          after: 'reconcileType'
        }, { transaction: t }),
        queryInterface.addColumn('FormFieldsOptions', 'duplicationOf', {
          type: Sequelize.INTEGER,
          after: 'height'
        }, { transaction: t }),
        queryInterface.addColumn('FormFieldsLogics', 'duplicationOf', {
          type: Sequelize.INTEGER,
          after: 'data'
        }, { transaction: t })
      ]);
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(t => {
      return Promise.all([
        queryInterface.removeColumn('Forms', 'duplicationOf', { transaction: t }),
        queryInterface.removeColumn('FormFields', 'duplicationOf', { transaction: t }),
        queryInterface.removeColumn('FormFieldsOptions', 'duplicationOf', { transaction: t }),
        queryInterface.removeColumn('FormFieldsLogics', 'duplicationOf', { transaction: t })
      ]);
    });
  }
};

Upvotes: 4

Related Questions