Reputation: 37
I'm trying to find all information through user's id in my project. I set column name 'order_userid' and i intended search this column. But I got error below. I didn't create 'userId' column. Do I need to make new column or fix migration file?
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.
This is the codes.
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');
}
};
migration file (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');
}
};
How can I solve this problem?
Upvotes: 0
Views: 1258
Reputation: 169
This is a problem with name mismatch, basically you've added the new column into the database using migration file as order_userid
and in model file of order which is used to query from sequlize ORM you have named the column as userId
.
So when sequlize fires the query it tries to fit the name which he has userId which database won't have. Please add model file in the question.
If you want a quick solution then change the code to, but I would suggest to make a proper solution:
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
}
})
Upvotes: 1