【发布时间】:2020-11-22 20:13:21
【问题描述】:
我有一个种子,我使用带有查询接口的 bulkDelete。没关系,但我需要截断表格。我设置了 truncate: true 选项,但出现错误:
无法截断外键约束中引用的表(vexus_panel.visits、CONSTRAINTvisits_ibfk_4 FOREIGN KEY (countryId) REFERENCES vexus_panel.countries (id))
然后我像这样启用级联:级联:真实但仍然无法正常工作
我的代码:
down: async (queryInterface, Sequelize) => {
/**
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
await queryInterface.bulkDelete("Countries", null, {
truncate: true,
cascade: true,
});
},
错误:
错误:无法截断外键约束中引用的表(
vexus_panel.visits,CONSTRAINTvisits_ibfk_4FOREIGN KEY (countryId) REFERENCESvexus_panel.countries(id))
更新:
我找到了解决办法。
down: async (queryInterface, Sequelize) => {
const { sequelize } = queryInterface;
try {
await sequelize.transaction(async (transaction) => {
const options = { transaction };
await sequelize.query("SET FOREIGN_KEY_CHECKS = 0", options);
await sequelize.query("TRUNCATE TABLE Countries", options);
await sequelize.query("SET FOREIGN_KEY_CHECKS = 1", options);
});
} catch (error) {
console.log(error);
}
}
【问题讨论】:
标签: node.js sequelize.js