gpt4 book ai didi

mysql - 在 Sequelize 上运行迁移会引发死锁错误

转载 作者:行者123 更新时间:2023-12-03 22:18:13 25 4
gpt4 key购买 nike

所以,我的项目有一个使用 Sequelize 设置的 MySQL 数据库,后端是用 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 上运行迁移会引发死锁错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67173083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com