如何使用 postgresql 删除带有 Sequelize.js 的所有表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28418499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I drop all tables with Sequelize.js using postgresql?
提问by Shamoon
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"
但我得到: 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?
我假设我不能在 postgres 中使用该关键字?但是有没有一种等效的方法来删除所有表并重新创建?
采纳答案by jorgenkg
This is an updated answer, targeted at the googlers who wound up here like me.
这是一个更新的答案,针对像我一样在这里结束的谷歌员工。
Sequelize offers a drop function:
Sequelize 提供了一个 drop 函数:
drop(options) => promise
Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model. Sequelize docs
删除通过此 sequelize 实例定义的所有表。这是通过在每个模型上调用 Model.drop 来完成的。续集文档
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
});
回答by aercolino
For wiping out data and create all again from scratch (like in tests):
用于清除数据并从头开始重新创建所有数据(如在测试中):
sequelize.sync({force: true});
回答by a_horse_with_no_name
I don't know anything about that JavaScript library, but Postgres provides a single command to drop everything that is owned by a user:
我对该 JavaScript 库一无所知,但 Postgres 提供了一个命令来删除用户拥有的所有内容:
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 notwant to drop.
这只会工作,如果一切都是由同一用户拥有,并且用户不必某些表(或视图,序列,...),你不希望下跌。
More details in the manual:
http://www.postgresql.org/docs/current/static/sql-drop-owned.html
手册中的更多详细信息:http:
//www.postgresql.org/docs/current/static/sql-drop-owned.html