Sling Academy
Home/Node.js/Fixing Sequelize Error: Unknown column ‘*.createdAt’ in ‘field list’

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

Last updated: December 29, 2023

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.

Next Article: Sequelize.js: How to Use Include with Attributes

Previous Article: How to Convert Entity to Plain Object in Sequelize.js

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates