英文:
Failed to save data into postgres database using sequelize.js, but system return column multiple times
问题
在尝试使用Sequelize的BlogModel.create()
系统将数据保存到postgres
数据库时,系统未能将数据保存到表中,并且服务器在控制台中多次返回createdat
和updatedat
列(请参见下文)。在模式中,我只添加了一次该列,有人能就此问题提供建议吗?
执行(默认):INSERT INTO "userBlogs"("id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt")VALUES(DEFAULT,$1,$2,$3,$4,$5,$6,$7)RETURNING "id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt";
// userBlogs.js
'use strict';
module.exports =(sequelize,DataTypes)=> {
const userBlogs = sequelize.define('userBlogs',{
id:{
type:DataTypes.INTEGER(10),
allowNull:false,
primaryKey:true,
autoIncrement:true
},
email:{
type:DataTypes.STRING(255),
allowNull:false
},
blogdetails:{
type:DataTypes.TEXT,
allowNull:false
},
tags:{
type:DataTypes.STRING(255),
allowNull:false
},
createdat:{
type:DataTypes.DATE,
allowNull:false,
defaultValue:DataTypes.NOW
},
updatedat:{
type:DataTypes.DATE,
allowNull:false,
defaultValue:DataTypes.NOW
}
},{
timestamps:true,
tableName:'userBlogs'
});
return userBlogs;
};
// server.js
const usersBlogSchema = require('./modals/userBlogs');
const BlogModel = usersBlogSchema(sequelize,DataTypes);
app.post('/service/createblogs',async(req,res,next)=> {
try {
const userEmail = req.body.email;
const blogDetails = req.body.blogValue;
const tags = req.body.tagValue;
if(Object.keys(req.body).length === 0){
res.status(403).json({fail:'无效的博客请求或博客请求为空!');
} else {
var requestData = {email:userEmail,blogdetails:blogDetails,tags:tags};
const createBlogRequest = await BlogModel.create(requestData);
res.status(200).json({success:true});
}
} catch(e){
console.log(e);
return next(e);
}
});
英文:
While trying to save data into postgres
database using sequelize BlogModel.create()
system failed to save the data into table and server is returning columns createdat, updatedat,
multiple times in console. ( please see below ). In the scheme I have added the column only once, can someone advise on this issue here ?
Executing (default): INSERT INTO "userBlogs" ("id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING "id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt";
//userBlogs.js
'use strict';
module.exports = (sequelize, DataTypes) => {
const userBlogs = sequelize.define('userBlogs', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
email: {
type: DataTypes.STRING(255),
allowNull: false
},
blogdetails: {
type: DataTypes.TEXT,
allowNull: false
},
tags: {
type: DataTypes.STRING(255),
allowNull: false
},
createdat: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW
},
updatedat: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW
}
}, {
timestamps: true,
tableName: 'userBlogs'
});
return userBlogs;
};
//server.js
const usersBlogSchema = require('./modals/userBlogs');
const BlogModel = usersBlogSchema(sequelize, DataTypes);
app.post('/service/createblogs', async (req, res, next)=> {
try {
const userEmail = req.body.email;
const blogDetails = req.body.blogValue;
const tags = req.body.tagValue;
if (Object.keys(req.body).length === 0) {
res.status(403).json({ fail: "Invalid blog request or blog request is blank !" });
} else {
var requestData = {email:userEmail, blogdetails:blogDetails, tags:tags };
const createBlogRequest = await BlogModel.create(requestData);
res.status(200).json({ success: true });
}
} catch (e) {
console.log(e)
return next(e);
}
});
答案1
得分: 1
createdAt 和 updatedAt 多次出现是因为您已添加了列 (createdAt 和 updatedAt),同时设置了 timestamps:true,timestamps 也会添加这两列,可以使用列或 timestamps。
英文:
Returning createdAt and updatedAt multiple times because you have added columns (createdAt and updatedAt )and also timestamps:true ,
timestamps also adds these both columns
use either columns or timestamps
'use strict';
module.exports = (sequelize, DataTypes) => {
const userBlogs = sequelize.define('userBlogs', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
email: {
type: DataTypes.STRING(255),
allowNull: false
},
blogdetails: {
type: DataTypes.TEXT,
allowNull: false
},
tags: {
type: DataTypes.STRING(255),
allowNull: false
},
}, {
timestamps: true,
tableName: 'userBlogs'
});
return userBlogs;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论