fan*_*ats 8 postgresql node.js express sequelize.js
我正在阅读文档,它提到了两种做upsert的方法:findOrCreate()或upsert().我阅读了描述,但我仍然不清楚它们的区别.
在upsert()接着说:
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.
Run Code Online (Sandbox Code Playgroud)
这是否upsert()明确要求我id在模型中定义一个UNIQUE主要字段?即:
var Something = sequelize.define("Something", { id: {
type: DataTypes.INTEGER,
primaryKey: true,
unique: true,
allowNull: false}});
Run Code Online (Sandbox Code Playgroud)
如果是这样,为什么没有findOrCreate()这个限制?
有人可以解释何时findOrCreate()和upsert()应该使用的用例,为什么upsert()在findOrCreate()不需要它时有独特的约束要求?
Yur*_*bin 17
.findOrCreate将使用您提供的参数查询数据库,如果没有匹配,它将使用相同的参数执行插入操作.而a的要点.upsert是更新或插入记录.在更新操作的情况下,逻辑指示您基于唯一键查找记录,并且只有在找到此唯一记录后,才会根据提供的参数更新其值.如果您无法找到唯一记录,那么您才会执行插入操作.
我希望这是有道理的.
| 归档时间: |
|
| 查看次数: |
9779 次 |
| 最近记录: |