My code is:
DB.sequelize.query('SET FOREIGN_KEY_CHECKS = 0').complete(function(err) {
if (err) {
return done(err);
}
DB.sequelize.drop();
return DB.sequelize.sync().complete(function(err) {
if (err) {
return done(err);
}
});
});
and I have some foreign key constraints, but I thought that the SET FOREIGN_KEY_CHECKS = 0
would ignore that and let me drop. Instead, the error that I get is: ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fails
For anyone using sequelize 3.15.x they have refactored their query method so that any options after the initial SQL statement are contained in one options object. Thus the answer would look like this:
DB
.sequelize
.query('SET FOREIGN_KEY_CHECKS = 0', {raw: true})
.then(function(results) {
DB.sequelize.sync({force: true});
});
Try the following.
DB
.sequelize
.query('SET FOREIGN_KEY_CHECKS = 0', null, {raw: true})
.success(function(results) {
DB.sequelize.sync({force: true});
});
The "force: true" option for sync will add "DROP TABLE IF EXISTS" to the create statements, so this should achieve what you are trying to do with the drop().
Its also worth considering this answer too: Sequelize doesn't create foreign keys as constraints.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With