mysql - SET FOREIGN_KEY_CHECKS = 0 在 Sequelize 迁移中不起作用
问题描述
这是我的功能:
up: queryInterface => {
return queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0')
.then(() => queryInterface.dropTable('my_table'));
},
此查询在表中有效:
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `my_table`;
错误是
无法删除或更新父行:外键约束失败
解决方案
如果要在 Sequelize 迁移中删除约束/外键,则需要使用以下命令:
// an example of table projects and users
await queryInterface.removeConstraint('projects', 'projects_ibfk_1'); // tableName, constraintId
await queryInterface.dropTable('users');
执行 'SET FOREIGN_KEY_CHECKS = 0' 更好。
如果您不知道约束 ID(在我的例子中是 projects_ibfk_1),请使用:
const refs = await queryInterface.getForeignKeyReferencesForTable('projects');
console.log(refs);
推荐阅读
- c# - 在邮件附件中发送后无法访问要写入的文件
- spring - 在 Spring 中动态注册 cassandra 集群
- angular - moment.diff 的负值
- react-virtualized - React-Virtualized 在 WindowScroller 中保持浮动表头
- frontend - 想实现 cdkTrapFocus 但它不起作用
- c# - 我怎样才能使我的数据库方法更具未来性?
- python-3.x - 始终通过生产者和消费者线程获得最新的价值
- wicket - 使用 AjaxFallBackLink 的 Wicket 8.x 迁移问题
- angular - 使用 firestore 和 RxJS 缓存 Observables
- python - 转置数据框并从不同列返回值