首页 > 解决方案 > 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,?,?,?,?,?,?);

标签: javascriptmysqlnode.jssequelize.js

解决方案


推荐阅读