18

I am trying:

if (process.NODE_ENV === 'test') {
  foreignKeyChecks = 0;
  forceSync = true;
} else {
  foreignKeyChecks = 1;
  forceSync = false;
}

global.db.sequelize.query("SET FOREIGN_KEY_CHECKS = " + foreignKeyChecks).then(function() {
  return global.db.sequelize.sync({
    force: forceSync
  });
}).then(function() {
  return global.db.sequelize.query('SET FOREIGN_KEY_CHECKS = 1');
}).then(function() {
  var server;
  console.log('Initialzed database on:');
  console.log(config.db);
  return server = app.listen(port, function() {
    return console.log("Server listening at http://" + (server.address().address) + ":" + (server.address().port));
  });
})["catch"](function(err) {
  return console.log('err', err);
});

module.exports = app;

But I get: SequelizeDatabaseError: unrecognized configuration parameter "foreign_key_checks"

I assume I can't have that keyword in postgres? But is there an equivalent way to drop all tables and recreate?

3
  • There is no such option foreign_key_checks in Postgres. Where in the manual did you find that?
    – user330315
    Commented Feb 9, 2015 at 22:14
  • I assumed not. I copied from some post and I assume they were using MySQL
    – Shamoon
    Commented Feb 9, 2015 at 22:15
  • It seems reading the manual is really a lost art.
    – user330315
    Commented Feb 9, 2015 at 22:17

6 Answers 6

18

This is an updated answer, targeted at the googlers who wound up here like me.

Sequelize offers a drop function:

drop(options) => promise

Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model. Sequelize docs

Example

var sequelize = new Sequelize(config.database, config.username, config.password, config);

var someModel = sequelize.define('somemodel', {
  name: DataTypes.STRING
});

sequelize
  .sync() // create the database table for our model(s)
  .then(function(){
    // do some work
  })
  .then(function(){
    return sequelize.drop() // drop all tables in the db
  });
13

For wiping out data and create all again from scratch (like in tests):

sequelize.sync({force: true});
2
  • You are wrong. It does not delete tables that exist in database but not in sequelize model list Commented Dec 30, 2020 at 12:39
  • 2
    Sequelize also has sequelize.truncate to truncate all tables for those that don't want to actually delete the tables, it could be faster than drop + recreate which .sync seems to do: stackoverflow.com/a/66985334/895245 Commented Apr 7, 2021 at 11:47
6

I don't know anything about that JavaScript library, but Postgres provides a single command to drop everything that is owned by a user:

drop owned by <our_user_name cascade

This will only work if everything is owned by the same user and that user doesn't have some tables (or views, sequences, ...) that you do not want to drop.

More details in the manual:
http://www.postgresql.org/docs/current/static/sql-drop-owned.html

3
  • Any way to turn off foreign key constraints when dropping tables?
    – Shamoon
    Commented Feb 10, 2015 at 2:45
  • @Shamoon: you don't need to. The cascade keyword will automatically drop all foreign keys.
    – user330315
    Commented Feb 10, 2015 at 6:45
  • I realize that I don't need to, but I'm trying to stay within the confines of the ORM
    – Shamoon
    Commented Feb 10, 2015 at 16:32
3

For anyone looking for a solution with sequelize-cli checkout this link Sequelize CLI:

You can just run:

sequelize_cli db:drop

sequelize_cli db:create

To create or drop your db using the cli tool. This way you will have a empty db.

1

drop connection with db and: $ npx sequelize_cli db:drop

0
sequelize-cli db:drop && sequelize-cli db:create && sequelize-cli db:migrate

Not the answer you're looking for? Browse other questions tagged or ask your own question.