我正在使用Postman使用Sequelize,node和express为我的React应用程序测试POST路由。我在
下收到此错误vectorizer=CountVectorizer()
X =vectorizer.fit_transform(docs)
matrix_terms = np.array(vectorizer.get_feature_names())
matrix_freq = np.asarray(X.sum(axis=0)).ravel()
tfidf_transformer=TfidfTransformer()
tfidf_matrix = tfidf_transformer.fit_transform(X)
我的表的架构如下
{
"name": "SequelizeDatabaseError",
"parent": {
"code": "ER_NO_DEFAULT_FOR_FIELD",
"errno": 1364,
"sqlState": "HY000",
"sqlMessage": "Field 'title' doesn't have a default value",
"sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
"parameters": [
"2019-12-01 00:50:42",
"2019-12-01 00:50:42"
]
},
"original": {
"code": "ER_NO_DEFAULT_FOR_FIELD",
"errno": 1364,
"sqlState": "HY000",
"sqlMessage": "Field 'title' doesn't have a default value",
"sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
"parameters": [
"2019-12-01 00:50:42",
"2019-12-01 00:50:42"
]
},
"sql": "INSERT INTO `Trips` (`id`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?);",
"parameters": [
"2019-12-01 00:50:42",
"2019-12-01 00:50:42"
]
}
我尝试将各个列更改为DEFAULT NULL,但是即使将数据输入到这些字段中,我也会在数据库中恢复为null。我添加了代码图片。
反应形式
Trips控制器
Trips模型
Trips路由器
-山姆
答案 0 :(得分:1)
您有2个问题:
1)在您的SQL声明和模型声明中,您缺少title
列的默认值。
您的SQL表声明应如下:
CREATE TABLE Trips (
id INT NOT NULL AUTO_INCREMENT,
title varchar(255) DEFAULT NULL, -- Or any other default value
location varchar(255) DEFAULT NULL,
Description varchar(255) DEFAULT NULL,
tripDate datetime DEFAULT NULL,
image varchar(255) DEFAULT NULL,
createdAt timestamp default current_timestamp,
updatedAt timestamp,
PRIMARY KEY (id)
);
根据此声明,您的模型声明应为:
const Trips = sequelize.define('Trips', {
title: {
type: DataTypes.STRING,
defaultValue: null // or whatever you would like
},
location:DataTypes.STRING,
Description:DataTypes.STRING,
tripDate:DataTypes.DATEONLY,
image:DataTypes.STRING
},
如果修改这两个后仍然出现此错误,则问题在客户端上 方面,由于某种原因,标题数据不会传递到服务器,因此是 在req.body中未定义
答案 1 :(得分:0)
1。默认情况下,它将在模型架构中查找createdAt,updatedAt。 因此,如果不想使用这两个字段,则可以添加createdAt,updatedAt,然后在模型架构中设置timestamps:false。
2。您应该在模型架构中添加id字段,因为sequelize始终需要在模型架构中使用具有主键的id字段。在每个模型中都是必需的。
const Trips = sequelize.define('Trips',{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title:DataTypes.STRING,
location:DataTypes.STRING,
Description:DataTypes.STRING,
tripDate:DataTypes.DATEONLY,
image:DataTypes.STRING
},
{timestamps:false});