要更新的代码:
return db.models.sprout.update({
first_name: args.input.patch.first_name,
last_name: args.input.patch.last_name,
date_of_birth: args.input.patch.date_of_birth,
image_url: args.input.patch.image_url,
updated_by: args.input.user_id
},{ where: { sprout_id : args.input.sprout_id }}).then((rowsUpdated) => {
表中所有字段都没有非空约束。
理想情况下,这应该仅更新在我仅提供的 image_url 中提供的值
我收到此错误。
notNull 违规: sprout.first_name 不能为 null,\nnotNull 违规: sprout.last_name 不能为 null
奇怪,插入按照我想要的方式工作,仅插入存在的内容
return db.models.sprout.create({
sprout_id: uuidv4(),
first_name: args.input.sprout.first_name,
last_name: args.input.sprout.last_name,
date_of_birth: args.input.sprout.date_of_birth,
image_url: args.input.sprout.image_url,
created_by: args.input.user_id
})
如果我不提供 image_url 或任何其他字段,插入工作正常,并忽略 null,但更新不会。
我如何进行更新以忽略空值。
最佳答案
sequelize中有这样一个东西:omitNull
,你应该将其设置为true。请在文档中查看:http://docs.sequelizejs.com/class/lib/model.js~Model.html
但是这里:https://github.com/sequelize/sequelize/issues/2352他们团队的工程师表示“omitNull 是一个肮脏的黑客行为,现在有更好的方法来解决它。”而且他没有推荐正确的方法。
就个人而言,我建议您在将对象传递给sequelize之前清除对象中的空字段。请看这个帖子:Remove blank attributes from an Object in Javascript
关于node.js - Sequelize 更新不会忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48322711/