sequelize.sync()上的force选项有什么作用?
sequelize.sync({
force: true
});
具体来说,我有兴趣了解什么力量:错误呢?它不会将模式与数据库同步吗?
是否有任何关于续集的正式文档?我只能在文档中找到示例。
Answers:
(或多或少)正式文档和API参考可在http://sequelize.readthedocs.org/en/latest/api/sequelize/#sync中找到
问题:在尝试创建表之前force: true
添加一个DROP TABLE IF EXISTS
-如果您强制执行,则现有表将被覆盖。
force: false
做什么,而不是做什么force: true
。
OP正在询问要做什么force: false
,这也是我想知道的内容,因此剩下的就是这些。
对我来说,主要的收获是各个字段没有同步(这正是我希望从水线ORM中获得的)。意思是,如果您拥有force: false
表并且该表存在,那么您执行的任何字段添加/修改/删除都将不会执行。
beforeSync
钩子运行force: true
if not exists
afterSync
钩子运行lib.model.js
Model.prototype.sync = function(options) {
options = options || {};
options.hooks = options.hooks === undefined ? true : !!options.hooks;
options = Utils._.extend({}, this.options, options);
var self = this
, attributes = this.tableAttributes;
return Promise.try(function () {
if (options.hooks) {
return self.runHooks('beforeSync', options);
}
}).then(function () {
if (options.force) {
return self.drop(options);
}
}).then(function () {
return self.QueryInterface.createTable(self.getTableName(options), attributes, options, self);
}).then(function () {
return self.QueryInterface.showIndex(self.getTableName(options), options);
}).then(function (indexes) {
// Assign an auto-generated name to indexes which are not named by the user
self.options.indexes = self.QueryInterface.nameIndexes(self.options.indexes, self.tableName);
indexes = _.filter(self.options.indexes, function (item1) {
return !_.some(indexes, function (item2) {
return item1.name === item2.name;
});
});
return Promise.map(indexes, function (index) {
return self.QueryInterface.addIndex(self.getTableName(options), _.assign({logging: options.logging, benchmark: options.benchmark}, index), self.tableName);
});
}).then(function () {
if (options.hooks) {
return self.runHooks('afterSync', options);
}
}).return(this);
};