Sequelize) 查询findAll时,从不存在的列中查找

Sequelize) When query findAll, it find from nonexistent column

我正在尝试通过我的项目中的用户 ID 查找所有信息。我设置了列名 'order_userid' 并打算搜索此列。但是我在下面遇到错误。我没有创建 'userId' 列。我是否需要创建新列或修复迁移文件?

Executing (default): SELECT `id`, `order_context`, `price`, `createdAt`, `updatedAt`, `userId` FROM `orders` AS `order` WHERE `order`.`order_userid` = 1;
(node:28648) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: Unknown column 'userId' in 'field list'
    at Query.formatError (/home/kyu/projects/Giftletters/server/node_modules/sequelize/lib/dialects/mysql/query.js:265:16)
    at Query.run (/home/kyu/projects/Giftletters/server/node_modules/sequelize/lib/dialects/mysql/query.js:77:18)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:28648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:28648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是代码。

 function (err, decoded) {
        
        if (err) {
          res.status(400).send(err)
        } else {
          order.findAll({
            where : {
              order_userid : decoded.id
            }
          })
              
'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('orders', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      order_context: {
        type: Sequelize.STRING
      },
      price: {
        type: Sequelize.INTEGER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('orders');
  }
};

迁移文件 (20210721023329-order_userid.js)

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.addColumn('orders', 'order_userid', Sequelize.INTEGER);

    await queryInterface.addConstraint('orders', {
      fields: ['order_userid'],
      type: 'foreign key',
      name: 'FK_from_user_to_order',
      references: {
        table: 'users',
        field: 'id'
      },
      onDelete: 'cascade',
      onUpdate: 'cascade'
    });

  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.removeConstraint('orders', 'FK_from_user_to_order');
    await queryInterface.removeColumn('orders', 'order_userid');
  }
};

我该如何解决这个问题?

这是名称不匹配的问题,基本上您已经使用迁移文件将新列添加到数据库中 order_userid 以及用于从您命名的 sequlize ORM 查询的订单模型文件列为 userId.

因此,当 sequlize 触发查询时,它会尝试匹配他拥有 userId 而数据库没有的名称。请在问题中添加模型文件。

如果您想要一个快速的解决方案,请将代码更改为,但我建议您制定一个合适的解决方案:

 function (err, decoded) {
        
        if (err) {
          res.status(400).send(err)
        } else {
          order.findAll({
            attributes: [`id`, `order_context`, `price`, `createdAt`, `updatedAt`],
            where : {
              order_userid : decoded.id
            }
          })