gpt4 book ai didi

mysql - sequelize 在数据库(MySql)中发布 NULL

转载 作者:搜寻专家 更新时间:2023-11-01 00:47:47 24 4
gpt4 key购买 nike

module.exports = function (sequelize, Sequelize) {

var User = sequelize.define('user', {
id: { autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER },
firstname: { type: Sequelize.STRING, notEmpty: true },
lastname: { type: Sequelize.STRING, notEmpty: true },
//username: { type: Sequelize.TEXT },
//about: { type: Sequelize.TEXT },
mobileno: { type: Sequelize.STRING },
email: { type: Sequelize.STRING, validate: { isEmail: true } },
password: { type: Sequelize.STRING, allowNull: false },
last_login: { type: Sequelize.DATE },
//status: { type: Sequelize.ENUM('active', 'inactive'), defaultValue: 'active' }

});

return User;
}

其他一切都正常发布,但 mobileno 在数据库中被发布为 null

我尝试将手机号码设置为 allowNull = false 但这给了我一个错误。

我也尝试过将字符串更改为文本,但这也无济于事......

enter image description here

这是添加allowNull=false后的错误..

最佳答案

检查 documentation 后您的代码工作正常。

 // setting allowNull to false will add NOT NULL to the column, which means an error will be
// thrown from the DB when the query is executed if the column is null. If you want to check that a value
// is not null before querying the DB, look at the validations section below.
title: { type: Sequelize.STRING, allowNull: false },

在保存到数据库之前,您需要验证您的信息以确保 mobileno 不是 null。一旦您获得了 mobileno 的值,您就可以将其保存到 db

关于mysql - sequelize 在数据库(MySql)中发布 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57002018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com