Skip to main content
Version: v6 - stable

升级到 v6

Sequelize v6 是 v5 之后的下一个主要版本。以下是可帮助你升级的重大更改列表。

¥Sequelize v6 is the next major release after v5. Below is a list of breaking changes to help you upgrade.

重大变化

¥Breaking Changes

支持 Node 10 及更高版本

¥Support for Node 10 and up

Sequelize v6 将仅支持 Node 10 及更高版本 #10821

¥Sequelize v6 will only support Node 10 and up #10821.

CLS

你现在应该使用 cls-hooked 包来支持 CLS。

¥You should now use cls-hooked package for CLS support.

const cls = require('cls-hooked');
const namespace = cls.createNamespace('....');
const Sequelize = require('sequelize');

Sequelize.useCLS(namespace);

数据库引擎支持

¥Database Engine Support

我们更新了支持的最低数据库引擎版本。使用较旧的数据库引擎将显示 SEQUELIZE0006 弃用警告。版本表请查看 发布页面

¥We have updated our minimum supported database engine versions. Using older database engine will show SEQUELIZE0006 deprecation warning. Please check the releases page for the version table.

Sequelize

  • 蓝鸟已被删除。在内部,所有方法现在都使用 async/await。公共 API 现在返回原生 promise。感谢 Andy Edwards 的重构工作。

    ¥Bluebird has been removed. Internally all methods are now using async/await. Public API now returns native promises. Thanks to Andy Edwards for this refactor work.

  • Sequelize.Promise 不再可用。

    ¥Sequelize.Promise is no longer available.

  • sequelize.import 方法已被删除。CLI 用户应更新到 sequelize-cli@6

    ¥sequelize.import method has been removed. CLI users should update to sequelize-cli@6.

  • QueryInterface 和 QueryGenerator 的所有实例都已重命名为它们的小驼峰式变体,例如。 当 queryInterface 和 queryGenerator 用作 Model 和 Dialect 上的属性名称时,类名称保持不变。

    ¥All instances of QueryInterface and QueryGenerator have been renamed to their lowerCamelCase variants eg. queryInterface and queryGenerator when used as property names on Model and Dialect, the class names remain the same.

模型

¥Model

options.returning

选项 returning: true 将不再返回模型中未定义的属性。旧的行为可以通过使用 returning: ['*'] 来实现。

¥Option returning: true will no longer return attributes that are not defined in the model. Old behavior can be achieved by using returning: ['*'] instead.

Model.changed()

Sequelize 不检测深度突变。为了避免 save 出现问题,你应该将每个属性视为不可变并且仅分配新值。

¥Sequelize does not detect deep mutations. To avoid problems with save, you should treat each attribute as immutable and only assign new values.

属性深度突变的示例:

¥Example with a deep mutation of an attribute:

const instance = await MyModel.findOne();

// Sequelize will not detect this change
instance.jsonField.jsonProperty = 12345;

console.log(instance.changed()); // false

// You can workaround this by telling Sequelize the property changed:
instance.changed('jsonField', true);
console.log(instance.changed()); // true

例如,如果你将每个属性视为不可变:

¥Example if you treat each attribute as immutable:

const instance = await MyModel.findOne();

// Sequelize will detect this change
instance.jsonField = {
...instance.jsonField,
jsonProperty: 12345,
};

console.log(instance.changed()); // true

Model.bulkCreate()

此方法现在抛出 Sequelize.AggregateError 而不是 Bluebird.AggregateError。所有错误现在都显示为 errors 键。

¥This method now throws Sequelize.AggregateError instead of Bluebird.AggregateError. All errors are now exposed as errors key.

Model.upsert()

所有方言现在都支持原生 upsert。

¥Native upsert is now supported for all dialects.

const [instance, created] = await MyModel.upsert({});

该方法的签名已更改为 Promise<Model,boolean | null>。第一个索引包含更新插入的 instance,第二个索引包含一个布尔值(或 null),指示记录是否已创建或更新。对于 SQLite/Postgres,created 值将始终是 null

¥Signature for this method has been changed to Promise<Model,boolean | null>. First index contains upserted instance, second index contains a boolean (or null) indicating if record was created or updated. For SQLite/Postgres, created value will always be null.

  • MySQL - 通过 ON DUPLICATE KEY UPDATE 实现

    ¥MySQL - Implemented with ON DUPLICATE KEY UPDATE

  • PostgreSQL - 与 ON CONFLICT DO UPDATE 一起实现

    ¥PostgreSQL - Implemented with ON CONFLICT DO UPDATE

  • SQLite - 与 ON CONFLICT DO UPDATE 一起实现

    ¥SQLite - Implemented with ON CONFLICT DO UPDATE

  • MSSQL - 使用 MERGE 语句实现

    ¥MSSQL - Implemented with MERGE statement

Postgres 用户注意事项: 如果 upsert 有效负载包含 PK 字段,则 PK 将用作冲突目标。否则,将选择第一个唯一约束作为冲突键。

¥Note for Postgres users: If upsert payload contains PK field, then PK will be used as the conflict target. Otherwise first unique constraint will be selected as the conflict key.

QueryInterface

addConstraint

该方法现在只需要 2 个参数,tableNameoptions。以前,第二个参数可以是要应用约束的列名列表,现在必须将该列表作为 options.fields 属性传递。

¥This method now only takes 2 parameters, tableName and options. Previously the second parameter could be a list of column names to apply the constraint to, this list must now be passed as options.fields property.

更新日志

¥Changelog

6.0.0-beta.7

  • docs(associations):属于许多通过表创建的

    ¥docs(associations): belongs to many create with through table

  • docs(query-interface):修复损坏的链接 #12272

    ¥docs(query-interface): fix broken links #12272

  • docs(sequelize):omitNull 仅适用于 CREATE/UPDATE 查询

    ¥docs(sequelize): omitNull only works for CREATE/UPDATE queries

  • 文档:异步 #12297

    ¥docs: asyncify #12297

  • 文档:响应 #12308

    ¥docs: responsive #12308

  • 文档:更新功能请求模板

    ¥docs: update feature request template

  • feat(postgres):原生更新插入 #12301

    ¥feat(postgres): native upsert #12301

  • feat(sequelize):允许从 url #12404 传递 dialectOptions.options

    ¥feat(sequelize): allow passing dialectOptions.options from url #12404

  • fix(include):检查是否通过模型 #12316 包含指定的属性

    ¥fix(include): check if attributes specified for included through model #12316

  • fix(model.destroy):返回 0 并截断 #12281

    ¥fix(model.destroy): return 0 with truncate #12281

  • fix(mssql):空订单数组生成无效的 FETCH 语句 #12261

    ¥fix(mssql): empty order array generates invalid FETCH statement #12261

  • fix(postgres):描述表 #12409 时正确解析枚举

    ¥fix(postgres): parse enums correctly when describing a table #12409

  • fix(query):确保 QueryTypes.RAW #12305 的返回签名正确

    ¥fix(query): ensure correct return signature for QueryTypes.RAW #12305

  • fix(query):保留日志器 #12328 的 cls 上下文

    ¥fix(query): preserve cls context for logger #12328

  • fix(query-generator):如果 options.group 为空,则不生成 GROUP BY 子句 #12343

    ¥fix(query-generator): do not generate GROUP BY clause if options.group is empty #12343

  • fix(reload):包括默认范围 #12399

    ¥fix(reload): include default scope #12399

  • fix(types):将关联添加到 OrderItem 类型 #12332

    ¥fix(types): add Association into OrderItem type #12332

  • fix(types):将 clientMinMessages 添加到选项接口 #12375

    ¥fix(types): add clientMinMessages to Options interface #12375

  • fix(types):选项 #12377 中的 transactionType

    ¥fix(types): transactionType in Options #12377

  • fix(types):在 "where" 子句 #12337 中添加对可选值的支持

    ¥fix(types): add support for optional values in "where" clauses #12337

  • fix(types):将缺失的字段添加到 'FindOrCreateType' #12338

    ¥fix(types): add missing fields to 'FindOrCreateType' #12338

  • 使固定:将缺少的 sql 和参数属性添加到某些查询错误 #12299

    ¥fix: add missing sql and parameters properties to some query errors #12299

  • 使固定:删除自定义检查 #12262

    ¥fix: remove custom inspect #12262

  • 重构:清理查询构建器 #12304

    ¥refactor: cleanup query generators #12304

6.0.0-beta.6

  • docs(add-constraint):options.fields 支持

    ¥docs(add-constraint): options.fields support

  • docs(association):文档 uniqueKey 属于多个 #12166

    ¥docs(association): document uniqueKey for belongs to many #12166

  • docs(association):options.through.where 支持

    ¥docs(association): options.through.where support

  • docs(association):使用 and 代替 '和' #12191

    ¥docs(association): use and instead of 'a nd' #12191

  • docs(association):使用正确的范围名称 #12204

    ¥docs(association): use correct scope name #12204

  • docs(manuals):避免重复的标头 ID #12201

    ¥docs(manuals): avoid duplicate header ids #12201

  • docs(model):纠正示例代码 #12137 中的语法错误

    ¥docs(model): correct syntax error in example code #12137

  • docs(query-interface):删除索引索引名称或属性 #11947

    ¥docs(query-interface): removeIndex indexNameOrAttributes #11947

  • docs(resources):添加 sequelize-guard 库 #12235

    ¥docs(resources): add sequelize-guard library #12235

  • docs(typescript):修复令人困惑的注释 #12226

    ¥docs(typescript): fix confusing comments #12226

  • docs(v6-guide):bluebird 删除 API 更改

    ¥docs(v6-guide): bluebird removal API changes

  • 文档:数据库版本支持信息 #12168

    ¥docs: database version support info #12168

  • 文档:删除剩余的蓝鸟参考 #12167

    ¥docs: remove remaining bluebird references #12167

  • feat(belongs-to-many):允许创建偏执连接表 #12088

    ¥feat(belongs-to-many): allow creation of paranoid join tables #12088

  • feat(belongs-to-many):偏执连接表 #12256 的 get/has/count

    ¥feat(belongs-to-many): get/has/count for paranoid join table #12256

  • feat(pool):公开 maxUses 池配置选项 #12101

    ¥feat(pool): expose maxUses pool config option #12101

  • feat(postgres):minify 包含超出限制 #11940 的别名

    ¥feat(postgres): minify include aliases over limit #11940

  • feat(sequelize):处理查询字符串主机值 #12041

    ¥feat(sequelize): handle query string host value #12041

  • fix(associations):确保所有生成的属性 #12258 的架构正确

    ¥fix(associations): ensure correct schema on all generated attributes #12258

  • fix(docs/instances):对增量 #12087 使用正确的变量

    ¥fix(docs/instances): use correct variable for increment #12087

  • fix(include):单独的查询不是子查询 #12144

    ¥fix(include): separate queries are not sub-queries #12144

  • fix(model):修复 bulkCreate #12163 中关联逻辑中的未链接 promise

    ¥fix(model): fix unchained promise in association logic in bulkCreate #12163

  • fix(model):updateOnDuplicate 处理复合键 #11984

    ¥fix(model): updateOnDuplicate handles composite keys #11984

  • fix(model.count):没有任何列的 unique 会生成无效的 SQL #11946

    ¥fix(model.count): distinct without any column generates invalid SQL #11946

  • fix(model.reload):忽略 options.where 并始终使用 this.where() #12211

    ¥fix(model.reload): ignore options.where and always use this.where() #12211

  • fix(mssql) 由于 BOOLEAN 列类型 #12090 导致插入记录失败

    ¥fix(mssql) insert record failure because of BOOLEAN column type #12090

  • fix(mssql):在查询构建器 #11994 中强制转换 sql_variant

    ¥fix(mssql): cast sql_variant in query generator #11994

  • fix(mssql):不要使用 OUTPUT INSERTED 进行更新而不返回 #12260

    ¥fix(mssql): dont use OUTPUT INSERTED for update without returning #12260

  • fix(mssql):FETCH/NEXT 查询中的重复订单 #12257

    ¥fix(mssql): duplicate order in FETCH/NEXT queries #12257

  • fix(mssql):为浮点 #11962 设置正确的比例

    ¥fix(mssql): set correct scale for float #11962

  • fix(mssql):乏味的 v9 需要连接调用 #12182

    ¥fix(mssql): tedious v9 requires connect call #12182

  • fix(mssql):引擎表和列 #12212 使用大写

    ¥fix(mssql): use uppercase for engine table and columns #12212

  • fix(pool):当引擎不受支持时显示弃用 #12218

    ¥fix(pool): show deprecation when engine is not supported #12218

  • fix(postgres):addColumn 支持 ARRAY(ENUM) #12259

    ¥fix(postgres): addColumn support ARRAY(ENUM) #12259

  • fix(query):不要绑定在全字 #12250 中使用的 $

    ¥fix(query): do not bind $ used within a whole-word #12250

  • fix(query-generator):处理基于子字符串的运算符 #12210 的字面量

    ¥fix(query-generator): handle literal for substring based operators #12210

  • fix(query-interface):允许为查询接口插入 #11931 传递 null

    ¥fix(query-interface): allow passing null for query interface insert #11931

  • fix(query-interface):允许在 IndexesOptions #12224 的字段中使用 sequelize.fn 和 sequelize.literal

    ¥fix(query-interface): allow sequelize.fn and sequelize.literal in fields of IndexesOptions #12224

  • fix(scope):不要修改原始范围定义 #12207

    ¥fix(scope): don't modify original scope definition #12207

  • fix(sqlite):多个主键导致语法错误 #12237

    ¥fix(sqlite): multiple primary keys results in syntax error #12237

  • fix(sync):将选项传递给所有查询方法 #12208

    ¥fix(sync): pass options to all query methods #12208

  • fix(typings):将 type_helpers 添加到文件列表 #12000

    ¥fix(typings): add type_helpers to file list #12000

  • fix(typings):正确的 Model.init 返回类型 #12148

    ¥fix(typings): correct Model.init return type #12148

  • fix(typings):fn 可分配给 #12040

    ¥fix(typings): fn is assignable to where #12040

  • fix(typings):getForeignKeysForTables 参数定义 #12084

    ¥fix(typings): getForeignKeysForTables argument definition #12084

  • fix(typings):让运算符接受日期范围 #12162

    ¥fix(typings): make between operator accept date ranges #12162

  • refactor(ci):改进数据库等待脚本 #12132

    ¥refactor(ci): improve database wait script #12132

  • refactor(tsd-test-setup):添加并设置 dtslint #11879

    ¥refactor(tsd-test-setup): add & setup dtslint #11879

  • 重构:将所有方言条件逻辑移至子类 #12217

    ¥refactor: move all dialect conditional logic into subclass #12217

  • 重构:删除 sequelize.import 辅助程序 #12175

    ¥refactor: remove sequelize.import helper #12175

  • 重构:使用原生版本 #12159

    ¥refactor: use native versions #12159

  • 重构:使用对象扩展而不是 Object.assign #12213

    ¥refactor: use object spread instead of Object.assign #12213

6.0.0-beta.5

  • fix(find-all):抛出空属性 #11867

    ¥fix(find-all): throw on empty attributes #11867

  • fix(types):queryInterface.addIndex #11844

  • fix(types):sequelize.query #11596 中的 plain 选项

    ¥fix(types): plain option in sequelize.query #11596

  • fix(types):正确的重载方法顺序 #11727

    ¥fix(types): correct overloaded method order #11727

  • fix(types):Sequelize.where #11843comparator arg

    ¥fix(types): comparator arg of Sequelize.where #11843

  • fix(types):修复 BelongsToManyGetAssociationsMixinOptions #11818

    ¥fix(types): fix BelongsToManyGetAssociationsMixinOptions #11818

  • fix(types):将 hooks 添加到 CreateOptions #11736

    ¥fix(types): adds hooks to CreateOptions #11736

  • fix(increment):损坏的查询 #11852

    ¥fix(increment): broken queries #11852

  • fix(associations):与非主目标键 #11778 进行多对多

    ¥fix(associations): gets on many-to-many with non-primary target key #11778

  • 使固定:正确选择 SRID(如果存在)#11763

    ¥fix: properly select SRID if present #11763

  • feat(sqlite):为 options.storage #11853 自动提供路径

    ¥feat(sqlite): automatic path provision for options.storage #11853

  • feat(postgres):idle_in_transaction_session_timeout 连接选项 #11775

    ¥feat(postgres): idle_in_transaction_session_timeout connection option #11775

  • feat(index):改进以支持运算符 #11934 的多个字段

    ¥feat(index): improve to support multiple fields with operator #11934

  • docs(transactions):修复 addIndex 示例和语法 #11759

    ¥docs(transactions): fix addIndex example and grammar #11759

  • docs(raw-queries):删除过时的信息 #11833

    ¥docs(raw-queries): remove outdated info #11833

  • docs(optimistic-locking):修复缺失的手册 #11850

    ¥docs(optimistic-locking): fix missing manual #11850

  • docs(model):findOne 返回空结果 #11762 的值

    ¥docs(model): findOne return value for empty result #11762

  • docs(model-querying-basics.md):添加一些逗号 #11891

    ¥docs(model-querying-basics.md): add some commas #11891

  • docs(manuals):修复缺失的模型定义页面 #11838

    ¥docs(manuals): fix missing models-definition page #11838

  • docs(manuals):广泛重写 #11825

    ¥docs(manuals): extensive rewrite #11825

  • docs(dialect-specific):添加 MSSQL 域身份验证示例 #11799

    ¥docs(dialect-specific): add MSSQL domain auth example #11799

  • docs(associations):修复 assocs 手册 #11888 中的拼写错误

    ¥docs(associations): fix typos in assocs manual #11888

  • docs(associations):修复拼写错误 #11869

    ¥docs(associations): fix typo #11869

6.0.0-beta.4

  • feat(sync):允许在与启用更改的 #11708 同步时绕过 drop 语句

    ¥feat(sync): allow to bypass drop statements when sync with alter enabled #11708

  • fix(model):在包含的型号 #11713 上注入 DependentVirtualAttrs

    ¥fix(model): injectDependentVirtualAttrs on included models #11713

  • fix(model):产生冲突...正确更新 #11666

    ¥fix(model): generate ON CONFLICT ... DO UPDATE correctly #11666

  • fix(mssql):优化 formatError RegEx #11725

    ¥fix(mssql): optimize formatError RegEx #11725

  • fix(types):添加 getForeignKeyReferencesForTable 类型 #11738

    ¥fix(types): add getForeignKeyReferencesForTable type #11738

  • fix(types):将 'restore' 钩子添加到类型 #11730

    ¥fix(types): add 'restore' hooks to types #11730

  • fix(types):将 'fieldMaps' 添加到 QueryOptions 类型 #11702

    ¥fix(types): added 'fieldMaps' to QueryOptions typings #11702

  • fix(types):将 isSoftDeleted 添加到模型 #11628

    ¥fix(types): add isSoftDeleted to Model #11628

  • fix(types):修复更新插入输入 #11674

    ¥fix(types): fix upsert typing #11674

  • fix(types):为字段 #11648 中的 getter 和 setter 指定 'this'

    ¥fix(types): specified 'this' for getters and setters in fields #11648

  • fix(types):在 UpdateOptions 接口 #11647 中添加偏执

    ¥fix(types): add paranoid to UpdateOptions interface #11647

  • fix(types):将 'as' 包含在 IncludeThroughOptions 定义 #11624

    ¥fix(types): include 'as' in IncludeThroughOptions definition #11624

  • fix(types):将 Includeable 添加到 IncludeOptions.include 类型 #11622

    ¥fix(types): add Includeable to IncludeOptions.include type #11622

  • fix(types):事务锁 #11620

    ¥fix(types): transaction lock #11620

  • fix(sequelize.fn):转义美元符号 (#11533) #11606

    ¥fix(sequelize.fn): escape dollarsign (#11533) #11606

  • fix(types):添加嵌套到 Includeable #11354

    ¥fix(types): add nested to Includeable #11354

  • fix(types):将日期添加到 #11612

    ¥fix(types): add date to where #11612

  • fix(types):添加 getDatabaseName (#11431) #11614

    ¥fix(types): add getDatabaseName (#11431) #11614

  • fix(types):摧毁 #11618 之前

    ¥fix(types): beforeDestroy #11618

  • fix(types):查询接口表架构 #11582

    ¥fix(types): query-interface table schema #11582

  • 文档:自述文件.md #11698

    ¥docs: README.md #11698

  • docs(sequelize):详细选项.重试使用 #11643

    ¥docs(sequelize): detail options.retry usage #11643

  • 文档:澄清 Sequelize 构造函数 #11653 中的日志记录选项

    ¥docs: clarify logging option in Sequelize constructor #11653

  • docs(migrations):修复示例 #11626 中的语法错误

    ¥docs(migrations): fix syntax error in example #11626

  • 文档:描述日志记录选项 #11654

    ¥docs: describe logging option #11654

  • docs(transaction):修复拼写错误 #11659

    ¥docs(transaction): fix typo #11659

  • docs(hooks):添加有关属于多个 #11601 的信息

    ¥docs(hooks): add info about belongs-to-many #11601

  • docs(associations):修复拼写错误 #11592

    ¥docs(associations): fix typo #11592

6.0.0-beta.3

  • 壮举:支持 cls-hooked / 测试 #11584

    ¥feat: support cls-hooked / tests #11584

6.0.0-beta.2

  • feat(postgres):将返回选项更改为仅返回模型属性 #11526

    ¥feat(postgres): change returning option to only return model attributes #11526

  • fix(associations):允许属于多个 #11578 的二进制密钥

    ¥fix(associations): allow binary key for belongs-to-many #11578

  • fix(postgres):始终替换 upsertQuery 的返回语句

    ¥fix(postgres): always replace returning statement for upsertQuery

  • fix(model):让 .changed() 深入了解 #10851

    ¥fix(model): make .changed() deep aware #10851

  • 改变:使用节点 10 #11580

    ¥change: use node 10 #11580