javascript - sequelize 中切换的数据字段
问题描述
当我使用 sequelize 在我的数据库中创建一个新行时,其中两个数据点最终出现在错误的列中,就像它们正在被切换一样。
这是我正在创建的内容:
Product.create({
title: 'title',
price: 'price',
description: 'description',
imageUrl: 'imageUrl'
}).then( result => {
console.log(result);
}).catch(err => {console.log(err)});
以下是数据库中的实际结果:
{
title: 'title',
price: price,
description: 'imageUrl', // switched with imageUrl
imageUrl: 'description' // switched with description
}
最令人困惑的是,记录到控制台的数据是正确的:
{
id: 15,
title: 'title',
price: 'price',
description: 'description',
imageUrl: 'url',
updatedAt: 2020-12-27T16:31:29.541Z,
createdAt: 2020-12-27T16:31:29.541Z
}
我究竟做错了什么??
编辑-这里要求的是模型定义:
const Product = sequelize.define('product', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
title: Sequelize.STRING,
price: {
type: Sequelize.DOUBLE,
allowNull: false
},
description: {
type: Sequelize.STRING,
allowNull: false
},
imageUrl: {
type: Sequelize.STRING,
allowNull: false
}
})
同样根据要求,这是 SQL 记录命令 sequelize 正在运行的结果:
Executing (default): SELECT `id`, `title`, `price`, `description`, `imageUrl`, `createdAt`, `updatedAt` FROM `products` AS `product`;
Executing (default): INSERT INTO `products` (`id`,`title`,`price`,`description`,`imageUrl`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?);
解决方案
推荐阅读
- c# - 在 IActionFilter OnActionExecuting() 中获取请求的大小
- php - 表单后数据未通过
- spring - Docker gradlew 构建失败并出现 HibernateException
- reactjs - React Chunk 下载背景
- keras - CNN 在良好表现和机会之间交替
- java - 无法使用 ListView 显示视频 - java.io.IOException:setDataSource failed
- firebase - user_pseudo_id 的计数与 Firebase 仪表板中显示的数字不同
- node.js - 在 nodejs 环境中的多个进程之间共享内存
- sql - 我们如何将 json 列表转换为 postgres 中的行?
- database - 用户离开时如何正确插入时间(user_left和user_joined得到相同的值)