Sequelize.js sequelize.sync()如何工作,特别是强制选项?

Sequelize.js sequelize.sync()如何工作,特别是强制选项?,sequelize.js,Sequelize.js,sequelize.sync()上的强制选项有什么作用 具体来说,我想知道force:false的作用是什么?它不会将架构与数据库同步吗 sequelize是否有正式文档?我只能在文档中找到示例。(或多或少)正式文档和API参考可在 对于您的问题:force:true在尝试创建表之前添加一个删除表(如果存在),如果您强制,现有表将被覆盖。OP询问的是force:false做了什么,这也是我想知道的,下面是其余内容 对我来说,主要的收获是各个字段没有同步(这正是我所希望的,来自水线ORM)。也就是

sequelize.sync()上的强制选项有什么作用

具体来说,我想知道force:false的作用是什么?它不会将架构与数据库同步吗

sequelize是否有正式文档?我只能在文档中找到示例。

(或多或少)正式文档和API参考可在


对于您的问题:
force:true
在尝试创建表之前添加一个
删除表(如果存在)
,如果您强制,现有表将被覆盖。

OP询问的是
force:false
做了什么,这也是我想知道的,下面是其余内容

对我来说,主要的收获是各个字段没有同步(这正是我所希望的,来自水线ORM)。也就是说,如果您有
force:false
且该表存在,则不会执行任何字段添加/修改/删除操作。

  • beforeSync
    hook运行之前
  • 如果
    force:true
  • 如果不存在,则使用
    创建表
  • 必要时添加索引
  • afterSync
    钩子运行
以下是参考资料:

lib.model.js

Model.prototype.sync=函数(选项){
选项=选项| |{};
options.hooks=options.hooks==未定义?true:!!options.hooks;
options=Utils.\扩展({},this.options,options);
var self=这个
,attributes=this.tableAttributes;
return Promise.try(函数(){
if(options.hooks){
返回self.runHooks('beforeSync',options);
}
}).然后(函数(){
if(options.force){
返回self.drop(选项);
}
}).然后(函数(){
返回self.QueryInterface.createTable(self.getTableName(选项)、属性、选项、self);
}).然后(函数(){
返回self.QueryInterface.showIndex(self.getTableName(options),options);
}).then(函数(索引){
//为用户未命名的索引指定自动生成的名称
self.options.index=self.QueryInterface.nameIndexes(self.options.index,self.tableName);
indexes=\过滤器(self.options.indexes,函数(item1){
返回!部分(索引、函数(项2){
返回item1.name==item2.name;
});
});
return Promise.map(索引、函数(索引){
返回self.QueryInterface.addIndex(self.getTableName(options),\u.assign({logging:options.logging,benchmark:options.benchmark},index),self.tableName);
});
}).然后(函数(){
if(options.hooks){
返回self.runHooks('afterSync',选项);
}
}).归还(本);
};

最小可运行示例

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: 'tmp.sqlite',
});
(async () => {
const IntegerNames = sequelize.define('IntegerNames', {
  value: { type: DataTypes.INTEGER, },
  name: { type: DataTypes.STRING, },
}, {});
//await IntegerNames.sync({force: true})
await IntegerNames.create({value: 2, name: 'two'});
await sequelize.close();
})();
设置:

npm install sequelize@6.5.1 sqlite3@5.0.2.
在stdout上,我们可以看到它所做的查询:

Executing (default): DROP TABLE IF EXISTS `IntegerNames`;
Executing (default): CREATE TABLE IF NOT EXISTS `IntegerNames` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `value` INTEGER, `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`IntegerNames`)
Executing (default): INSERT INTO `IntegerNames` (`id`,`value`,`name`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4);
如果我们使用
force:false
,我们得到的结果是相同的,只是在开始时没有
DROP

Executing (default): CREATE TABLE IF NOT EXISTS `IntegerNames` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `value` INTEGER, `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`IntegerNames`)
Executing (default): INSERT INTO `IntegerNames` (`id`,`value`,`name`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4);
如果删除同步,则根本不会创建表:

Executing (default): INSERT INTO `IntegerNames` (`id`,`value`,`name`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3,$4);
通过以下方式进行测试:

npm install sequelize@6.5.1 sqlite3@5.0.2.
force:false
无法单独同步架构,您需要
alter:true

使用
force:true
,数据库会被删除并重新创建,因此它当然会匹配最新的模式,但会丢失所有数据

为了保留现有数据和更新模式,除了使用
force:false
之外,我们还必须使用
alter:true

下面的works和stdout显示了通过创建临时数据库
IntegerNames\u backup
使其工作的复杂查询序列:

const assert = require('assert')
const { Sequelize, DataTypes } = require('sequelize');

(async () => {
{
  const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: 'tmp.sqlite',
  });
  const IntegerNames = sequelize.define('IntegerNames', {
    value: { type: DataTypes.INTEGER, },
    name: { type: DataTypes.STRING, },
  }, {});
  await IntegerNames.sync({force: true})
  await IntegerNames.create({value: 2, name: 'two'});
  await IntegerNames.create({value: 3, name: 'three'});
  await sequelize.close();
}

// Alter by adding column..
{
  const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: 'tmp.sqlite',
  });
  const IntegerNames = sequelize.define('IntegerNames', {
    value: { type: DataTypes.INTEGER, },
    name: { type: DataTypes.STRING, },
    nameEs: { type: DataTypes.STRING, },
  }, {});
  await IntegerNames.sync({
    alter: true,
    force: false,
  })
  await IntegerNames.create({value: 5, name: 'five' , nameEs: 'cinco'});
  await IntegerNames.create({value: 7, name: 'seven', nameEs: 'siete'});
  const integerNames = await IntegerNames.findAll({
    order: [['value', 'ASC']],
  });
  assert(integerNames[0].value  === 2);
  assert(integerNames[0].name   === 'two');
  assert(integerNames[0].nameEs === null);
  assert(integerNames[1].name   === 'three');
  assert(integerNames[1].nameEs === null);
  assert(integerNames[2].name   === 'five');
  assert(integerNames[2].nameEs === 'cinco');
  assert(integerNames[3].name   === 'seven');
  assert(integerNames[3].nameEs === 'siete');
  await sequelize.close();
}
})();
如果我们删除
alter:true
它会爆炸,因为新列不存在:

SequelizeDatabaseError: SQLITE_ERROR: table IntegerNames has no column named nameEs

然而,在实际项目中,迁移往往是更新架构的推荐方式:

此外:如果更改实体的表名(已经存在),则该表将保留数据,并创建另一个具有新名称的表。这并不能回答问题。这个问题问的是
force:false
做了什么,而不是
force:true
做了什么。我调用sequelize.sync({force:false}),但仍然会重新创建表。为什么会这样?
SequelizeDatabaseError: SQLITE_ERROR: table IntegerNames has no column named nameEs