0
I am testing the REST API I am developing. I am using Sequelize and Postgresql. In the API there is the table users
with the following fields:
user_id
user_nickname
user_fullname
user_password
created_at
updated_at
When trying to insert or return users the following error is pointed out in the Nodejs console:
Executing (default): SELECT "id", "user_nickname", "user_fullname", >"user_password", "created_at" AS "createdAt", "updated_at" AS "updatedAt" FROM >"users" AS "User"; Unhandledpromiserejectionwarning: Sequelizedatabaseerror: column "id" does not >exist
The Migration made through Sequelize was the following:
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.createTable('users', {
user_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
user_nickname: {
type: Sequelize.STRING,
allowNull: false
},
user_fullname:{
type: Sequelize.STRING,
allowNull: false
},
user_password: {
type: Sequelize.STRING,
allowNull: false
},
created_at: {
type: Sequelize.DATE,
allowNull: false
},
updated_at: {
type: Sequelize.DATE,
allowNull: false
}
});
},
down: async (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
}
};
And the inherited class of Model
is the following:
const { Model, DataTypes } = require('sequelize')
class User extends Model {
static init(connection) {
super.init({
user_nickname: DataTypes.STRING,
user_fullname: DataTypes.STRING,
user_password: DataTypes.STRING
}, {
sequelize: connection
})
}
}
module.exports = User
The query is being made like this:
async findAll(req, res) {
const users = await User.findAll()
return res.json(users)
}
I entered some users by the database itself to try to make a SELECT through Sequelize in the API, but results in the same error.
The consultation is apparently wrong. You did
SELECT "id", ...
while you should be wearingSELECT "user_id"...
– Augusto Vasques