column-relation-sequelize

Column “updated_at”/ “created_at” of relation does not exist in sequelize

In sequelize, when we create a model, there are two columns that are automatically added to our model i.e. updated_at and created_at. While defining a model, if we don’t provide these two fields then we get an error saying that column updated_at/created_at of relation does not exist in sequelize and we keep on thinking that it should not be an error because we don’t want these two fields.

Let’s take an example of the UserSearch model that will throw the above error:

const UserSearch = sequelize.define(
 'UserSearch',
 {
   id: {
     allowNull: false,
     autoIncrement: true,
     primaryKey: true,
     type: DataTypes.INTEGER,
   },
   userId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'user_id',
   },
   searchId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'search_id',
   },
   type: {
     type: DataTypes.STRING,
     allowNull: false
   },
   createdAt: {
     type: 'TIMESTAMP',
     defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
     allowNull: false,
     field: 'created_at'
   },
 },
 {
   tableName: 'user_search',
   underscored: true
 }
);

We can observe in the above model that we have defined created_at but we have not defined updated_at so when we try to add data to the above model. We get an error saying column “updated_at” of relation does not exist.

There are actually following two ways to fix this problem:

1. You can add timestamps as false to stop the default behavior of sequelize as shown below code.

const UserSearch = sequelize.define(
 'UserSearch',
 {
   id: {
     allowNull: false,
     autoIncrement: true,
     primaryKey: true,
     type: DataTypes.INTEGER,
   },
   userId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'user_id',
   },
   searchId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'search_id',
   },
   type: {
     type: DataTypes.STRING,
     allowNull: false
   },
   createdAt: {
     type: 'TIMESTAMP',
     defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
     allowNull: false,
     field: 'created_at'
   },
 },
 {
   tableName: 'user_search',
   underscored: true,
   timestamps: false
 }
);

2. The second way is that you can add the fields created_at/updated_at in the existing model.

const UserSearch = sequelize.define(
 'UserSearch',
 {
   id: {
     allowNull: false,
     autoIncrement: true,
     primaryKey: true,
     type: DataTypes.INTEGER,
   },
   userId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'user_id',
   },
   searchId: {
     type: DataTypes.INTEGER,
     allowNull: false,
     field: 'search_id',
   },
   type: {
     type: DataTypes.STRING,
     allowNull: false
   },
   createdAt: {
     type: 'TIMESTAMP',
     defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
     allowNull: false,
     field: 'created_at'
   },
   updatedAt: {
     type: 'TIMESTAMP',
     defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
     allowNull: false,
     field: 'updated_at'
   },
 },
 {
   tableName: 'user_search',
   underscored: true
 }
);

The more preferred way is the second way because the second way is more convenient for creating a migration file. Also, In later stages, we might need some analytics where these dates can play an important role.

Hope this article helps!!

Also read –
Javascript Interview Questions I was asked !!
How to get the date range between the two dates using moment.js?
How to split an array into equal chunks in javascript?

Leave a Comment