mysql - ValidationError:“列”在空列上不允许为空
问题描述
尝试设置为时passwordResetToken
,null
我不允许为空错误。如果我手动对数据库运行查询,则没有问题。
更新用户的完整代码如下:
let user = await this.usersRepository.getById(userId);
// ...
const userData = {
encryptedPassword: await bcrypt.hash(
newPasswd,
Number(process.env.BCRYPT_ROUNDS)
),
passwordResetToken: null,
passwordResetExpires: null
};
user = await this.usersRepository.update(user.id, userData);
我有一个这样定义的模型
const User = sequelize.define(
'user',
{
forename: DataTypes.STRING,
surname: DataTypes.STRING,
email: DataTypes.STRING,
encryptedPassword: DataTypes.STRING,
passwordResetToken: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
validate: {
notEmpty: false
}
},
passwordResetExpires: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null,
validate: {
isDate: true
}
}
},
{
classMethods: {
associate() {
// associations can be defined here
}
}
}
);
如您所见allowNull
,为真,defaultValue
为空并validate: notEmpty
设置为假。我错过了什么?
使用续集 v4.42.0
方言“mysql”
解决方案
这实际上是由我通过 npm 模块结构验证的另一部分引起的。
empty
我必须在我的架构中设置为 true,如下所示:
passwordResetToken: {
type: String,
required: false,
empty: true
},
推荐阅读
- liquibase - Liquibase:如何使用
变更集具有 runAlways true? - drop-down-menu - 有没有办法在下拉菜单中修改字体?
- opengl - OpenGL:单个三角形平面的正面和背面是否有不同的光效?
- google-cloud-functions - 从 CLI 触发 Firebase 模拟器中的 PubSub 事件
- python - 从 django.utils.encoding 导入 python_2_unicode_compatible ImportError: cannot import name 'python_2_unicode_compatible'
- reactjs - Lodash 延迟函数不允许我看到被调用函数内部的状态
- javascript - 优化在 NodeJs 中执行链式 API 调用的时间
- c - 如何在 rot13 c 程序中修复 f 字母等错误
- scala - 使用 spark-hbase 时,我得到了 ClassNotFoundException:org.apache.hadoop.hbase.spark.SparkSQLPushDownFilter
- macos - 写入 /Users/yourname/.zshrc 时出错:权限被拒绝