Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I drop all tables with Sequelize.js using postgresql?

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?

like image 778
Shamoon Avatar asked Feb 09 '15 20:02

Shamoon


People also ask

How do I delete all data from a table in Sequelize?

To delete rows of data from your SQL table using Sequelize, you need to use the provided destroy() method. The destroy() method can be called from any Model or instance of your Model to delete rows from your table.

Can you use Sequelize with Postgres?

For this application, we'll use Sequelize as ORM, as it supports multiple dialects, one of which is PostgreSQL. Sequelize provides a comfortable API to work with PostgreSQL databases from setup to execution, but there are many ORMs (e.g. TypeORM, Objection. js) to choose from for a Node.

How do I sync all models Sequelize?

A model can be synchronized with the database by calling model. sync(options) , an asynchronous function (that returns a Promise). With this call, Sequelize will automatically perform an SQL query to the database. Note that this changes only the table in the database, not the model in the JavaScript side.

What is DB Sequelize sync ()?

The Sequelize instance method sync() is used to synchronize your Sequelize model with your database tables. The synchronization happens at the table level. When your table doesn't exist the sync() method will generate and run a CREATE TABLE statement for you.


2 Answers

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
  });
like image 80
jorgenkg Avatar answered Oct 21 '22 19:10

jorgenkg


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

sequelize.sync({force: true});
like image 22
aercolino Avatar answered Oct 21 '22 21:10

aercolino