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?

Shamoon
  • 41,293
  • 91
  • 306
  • 570

5 Answers5

17

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
  • 4,140
  • 1
  • 34
  • 48
12

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

sequelize.sync({force: true});
aercolino
  • 2,193
  • 1
  • 22
  • 20
  • You are wrong. It does not delete tables that exist in database but not in sequelize model list – Muhammed Aydogan Dec 30 '20 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: https://stackoverflow.com/a/66985334/895245 – Ciro Santilli OurBigBook.com Apr 07 '21 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

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
  • 845
  • 9
  • 19
1

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