如何使用postgresql删除Sequelize.js的所有表?

Sha*_*oon 11 postgresql sequelize.js

我在尝试:

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;
Run Code Online (Sandbox Code Playgroud)

但我得到: SequelizeDatabaseError: unrecognized configuration parameter "foreign_key_checks"

我假设我在postgres中没有关键字?但有没有相同的方法来删除所有表并重新创建?

jor*_*nkg 8

这是一个更新的答案,针对像我一样在这里结束的谷歌.

Sequelize提供掉落功能:

drop(options) => promise
Run Code Online (Sandbox Code Playgroud)

删除通过此sequelize实例定义的所有表.这是通过在每个模型上调用Model.drop来完成的.Sequelize docs

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
  });
Run Code Online (Sandbox Code Playgroud)


aer*_*ino 8

用于清除数据并从头开始重新创建所有数据(如在测试中):

sequelize.sync({force: true});
Run Code Online (Sandbox Code Playgroud)

  • Sequelize 还具有 `sequelize.truncate` 来截断那些不想实际删除表的所有表,它可能比 `.sync` 似乎可以执行的 drop + recreate 更快:https://stackoverflow.com/ a/66985334/895245 (2认同)

a_h*_*ame 5

我对该JavaScript库一无所知,但Postgres提供了一个命令来删除用户拥有的所有内容:

drop owned by <our_user_name cascade
Run Code Online (Sandbox Code Playgroud)

仅当所有内容均由同一用户拥有并且该用户没有不想删除的某些表(或视图,序列等)时,这才起作用。

手册中的更多详细信息:http :
//www.postgresql.org/docs/current/static/sql-drop-owned.html