Reputation:
i have a migration, i only have four colums, id, name, last_name and email but when i do a query from postman it show me other colums over SELECT \"id\", \"name\", \"lastName\", \"email\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\"
what is the wrong?
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('User', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
last_name: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
});
},
down: (queryInterface) => {
return queryInterface.dropTable('User');
}
};
and when i used my service
static async getAllUsers() {
try {
const users = await database.User.findAll();
console.log('COnsOLE ', users)
return users
} catch (error) {
throw error;
}
}
i get this error from postman:
{
"status": "error",
"message": {
"name": "SequelizeDatabaseError",
"parent": {
"length": 104,
"name": "error",
"severity": "ERROR",
"code": "42P01",
"position": "73",
"file": "parse_relation.c",
"line": "1180",
"routine": "parserOpenTable",
"sql": "SELECT \"id\", \"name\", \"lastName\", \"email\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\";"
},
"original": {
"length": 104,
"name": "error",
"severity": "ERROR",
"code": "42P01",
"position": "73",
"file": "parse_relation.c",
"line": "1180",
"routine": "parserOpenTable",
"sql": "SELECT \"id\", \"name\", \"lastName\", \"email\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\";"
},
"sql": "SELECT \"id\", \"name\", \"lastName\", \"email\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\";"
}
}
i before used this commands many times: sequelize db:migrate
and sequelize db:migrate:undo
this is my git repository: https://github.com/x-rw/basePostgresExpressjs
you should situate in server directory and write npm run dev
Upvotes: 3
Views: 179
Reputation: 1918
That's because your sequelize model instance have updatedAt and createdAt fields and because of that it queries the database to get these two fields too. But in your migration file there is no updatedAt and createdAt fields. So your database table does not have these columns.
You have two options, if you really don't want to use updatedAt and createdAt you should specify that while initializing your sequelize model instance. Check the api reference. You should see options.timestamps. You can set it to false.
class YourModel extends Sequelize.Model { }
YourModel.init(
{
name: {
type: Sequelize.DataTypes.STRING(100),
allowNull: false,
validate: {
notNull: true,
notEmpty: true,
len: [2, 100]
}
},
},
{
sequelize: sequelizeInstance,
timestamps: false // This is what you need.
}
);
If you want to use it however, check my answer here to generate correct migrations.
Upvotes: 3