Sequelize.js: ER_ROW_IS_REFERENCED: unable to delete or update parent row: foreign key constraint fails

My code:

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 I SET FOREIGN_KEY_CHECKS = 0

would ignore that and let me opt out. Instead, I get the following message:ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fails

+3


source to share


2 answers


For those using sequelize 3.15.x, they refactored their query method so that any parameters after the original SQL statement are contained in a single parameter object, so the answer looks like this:



DB
    .sequelize
    .query('SET FOREIGN_KEY_CHECKS = 0', {raw: true})
    .then(function(results) {
        DB.sequelize.sync({force: true});
    });

      

+5


source


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" parameter for synchronization will add "DROP TABLE IF EXISTS" to the create statements, so this should achieve what you are trying to do with the drop () function.

Also worth considering this answer: Sequelize does not create foreign keys as constraints .

+5


source







All Articles