Sequelize支持插入重复密钥更新?

有没有一个模型或实例方法将执行插入或更新,取决于是否存在logging? 最好使用MySQL的“INSERT ON DUPLICATE KEY UPDATE”语法?

他们最近添加了这个特性upsert或者insertOrUpdate

  /** * Insert or update a single row. An update will be executed if a row * which matches the supplied values on either the primary key or a unique * key is found. Note that the unique index must be defined in your sequelize * model and not just in the table. Otherwise you may experience a unique * constraint violation, because sequelize fails to identify the row that * should be updated. */ Model.upsert({uniqueKey: 1234, name: 'joe'}).then(function () { // cheer for joy }); 

Sequelize目前不支持upsert – 我相信很难推出一个很好的跨语言解决scheme。

你可以做一个findOrCreate和一个updateAttributes。

编辑:Sequelize现在支持一个非常体面的交叉方言实施UPSERT,请参阅: https : //github.com/sequelize/sequelize/pull/2518