运行 Sequelize 上的迁移引发死锁错误
Running a migration on Sequelize is throwing a deadlock error
所以,我的项目有一个 MySQL 使用 Sequelize 设置的数据库,后端是用 nodeJS 编写的。
我正在尝试执行到我的数据库的简单迁移,将新列添加到 4 个不同的表。
当我 运行 迁移时,出现以下错误:
== 20210419115251-add-duplication-of-column-feature: migrating =======
ERROR: Deadlock found when trying to get lock; try restarting transaction
这是我的代码:
'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')
]);
}
};
我错过了什么?
非常感谢
尝试用 Sequelize 事务包装它以确保一切都像这样成功执行:
您可以在他们的文档页面 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 })
]);
});
}
};
所以,我的项目有一个 MySQL 使用 Sequelize 设置的数据库,后端是用 nodeJS 编写的。 我正在尝试执行到我的数据库的简单迁移,将新列添加到 4 个不同的表。
当我 运行 迁移时,出现以下错误:
== 20210419115251-add-duplication-of-column-feature: migrating =======
ERROR: Deadlock found when trying to get lock; try restarting transaction
这是我的代码:
'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')
]);
}
};
我错过了什么? 非常感谢
尝试用 Sequelize 事务包装它以确保一切都像这样成功执行:
您可以在他们的文档页面 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 })
]);
});
}
};