首页 > 解决方案 > Sequelize) 查询 findAll 时,从不存在的列中查找

问题描述

我正在尝试通过我的项目中的用户 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');
  }
};

我怎么解决这个问题?

标签: sequelize.js

解决方案


这是名称不匹配的问题,基本上您已经使用迁移文件将新列添加到数据库中,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
            }
          })

推荐阅读