如何在Sequelize 4中对upsert / create运行不同的验证

时间:2018-06-25 16:56:17

标签: javascript ecmascript-6 sequelize.js

我有一个要在其上运行一些功能的用户模型:

update password,
upsert user,
create user

但是,使用Sequelize 4时,我无法使用User.upsert(user)编辑用户。它要求所有字段都存在,而不是我在fields中定义的字段,如文档中所述:

const user = {password: 'eg', validate: 'eg', id: '123456789'}
return User.upsert(user, {
    fields: ['password', 'verify']
})
// "error": "null value in column \"email\" violates not-null constraint"

这意味着我必须User.findById(user.id)才能获得具有所有字段的用户,以便可以向用户添加相同的数据,通过验证并运行挂钩。

基本上,我需要将not-null约束设为false才能进行上更新和更新,而需要true来创建新记录。

有人知道该怎么做吗?

0 个答案:

没有答案