15

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?

Shamoon
  • 38,429
  • 77
  • 269
  • 518

4 Answers4

16

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
  });
jorgenkg
  • 3,950
  • 1
  • 32
  • 46
10

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

sequelize.sync({force: true});
aercolino
  • 1,934
  • 1
  • 19
  • 20
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

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
1

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.

briancollins081
  • 658
  • 6
  • 17