sequelize.js - 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');
}
};
我怎么解决这个问题?
解决方案
这是名称不匹配的问题,基本上您已经使用迁移文件将新列添加到数据库中,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
}
})
推荐阅读
- reactjs - 反应错误显示在控制台而不是窗口中
- javascript - DT::datatable - 选择要删除的行并写入没有闪亮的 csv
- javascript - 如何在没有表单且不出现错误 400 的情况下发出发布请求
- python - 在 Netty 中解码 PUT 请求
- docker - 通过 crontab 运行“docker run”命令不起作用
- xml - 尝试将 StaxSource 转换为 StreamResult 时,Xml Transformer 给我一个错误
- c# - 在绑定列表中查找对象索引?
- jquery - 是否可以在 jQuery 中自动将类添加到大写字母?
- flutter - 小部件列表未显示在屏幕上
- c++ - 当 StringSink 使用的 String 超出范围时程序崩溃