简体   繁体   English

ValidationError:“ null”列上的“ column”不允许为空

[英]ValidationError: “column” is not allowed to be empty on null column

When trying to set passwordResetToken to null I get not allowed to be empty errors. 当尝试将passwordResetToken设置为null我不允许为空错误。 If I manually run the query against the db, no trouble. 如果我对数据库手动运行查询,就没有麻烦了。

The full code to update user is as follows: 更新用户的完整代码如下:

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);

I have a model defined as such 我有一个这样定义的模型

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
        }
    }
}
);

As you can see allowNull is true, defaultValue is null and validate: notEmpty set to false. 如您所见, allowNull为true, defaultValue为null并validate: notEmpty设置为false。 What am I missing? 我想念什么?

Using sequelize v4.42.0 使用sequelize v4.42.0

Dialect 'mysql' 方言'mysql'

This was actually caused by another part of my validation by npm module structure . 这实际上是由我通过npm模块结构验证的另一部分引起的。

I had to set empty to true in my schema like this: 我必须在我的模式中将empty设置为true,如下所示:

  passwordResetToken: {
    type: String,
    required: false,
    empty: true
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM