Fixing Sequelize Error: Unknown column ‘*.createdAt’ in ‘field list’

Updated: December 29, 2023 By: Guest Contributor Post a comment

Understanding the Error

The error ‘Sequelize Error: Unknown column ‘*.createdAt’ in ‘field list” typically occurs when Sequelize, an ORM for Node.js, is attempting to query a column called ‘.createdAt’ that does not exist in the database. By default, Sequelize automatically adds the ‘createdAt’ and ‘updatedAt’ timestamps to models unless instructed otherwise. If these fields were not created in the database table, or if the naming conventions have been altered without proper configuration in Sequelize, this error can arise.

Fixing the Error

To fix the error, you can either add the missing columns to the database or configure Sequelize to match the existing database schema. Let’s start with the approach of modifying the Sequelize model configuration.

First, locate the model definition in your Node.js project that corresponds to the table causing the error. It would look something like the code block below, which defines a simple user model:

const User = sequelize.define('user', { /*...model attributes...*/ });

If you do not have ‘createdAt’ and ‘updatedAt’ columns in your table and wish to instruct Sequelize not to use these fields, you can disable them by setting the ‘timestamps’ option to false within the model definition:

const User = sequelize.define('user', { /*...model attributes...*/ }, { timestamps: false });

This change tells Sequelize not to expect these timestamp fields when performing database operations.

If the columns are present in the database but have different names, you can customize the timestamp field names in the model definition as follows:

const User = sequelize.define('user', { /*...model attributes...*/ }, { createdAt: 'creationDate', updatedAt: 'lastUpdatedDate' });

Make sure to replace ‘creationDate’ and ‘lastUpdatedDate’ with the actual column names from your database.

Another way to resolve the issue is by altering the database schema to include the missing ‘createdAt’ and ‘updatedAt’ columns if they are intended to be part of your model. You can use a database migration tool or manually run an ALTER TABLE command to add these columns.

After updating either the model definition or the schema, test your application thoroughly to confirm that the error is resolved and that the application functions as expected with the new configuration.