Sequelize Unknown column '*.createdAt' in 'field list'

node.jssequelize.js

node.js Problem Overview


I'm getting a Unknown column 'userDetails.createdAt' in 'field list' When trying to fetch with association.

Using findAll without association works fine.

My code is as follows:

var userDetails = sequelize.define('userDetails', {
    userId :Sequelize.INTEGER,
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
    birthday : Sequelize.DATE
});

var user = sequelize.define('user', {
    email: Sequelize.STRING,
    password: Sequelize.STRING
});

user.hasOne(userDetails, {foreignKey: 'userId'});

user.findAll({include: [userDetails] }).success(function(user) {
    console.log(user)
});

node.js Solutions


Solution 1 - node.js

I think the error is that you have timestamps enabled in sequelize, but your actual table definitions in the DB do not contain a timestamp column.

When you do user.find it will just do SELECT user.*, which only takes the columns you actually have. But when you join, each column of the joined table will be aliased, which creates the following query:

SELECT `users`.*, `userDetails`.`userId` AS `userDetails.userId`,`userDetails`.`firstName` AS `userDetails.firstName`,`userDetails`.`lastName` AS `userDetails.lastName`, `userDetails`.`birthday` AS `userDetails.birthday`, `userDetails`.`id` AS `userDetails.id`, `userDetails`.`createdAt` AS `userDetails.createdAt`, `userDetails`.`updatedAt` AS `userDetails.updatedAt` FROM `users` LEFT OUTER JOIN `userDetails` AS `userDetails` ON `users`.`id` = `userDetails`.`userId`;

The fix would be to disable timestamps for either the userDetails model:

var userDetails = sequelize.define('userDetails', {
    userId :Sequelize.INTEGER,
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
    birthday : Sequelize.DATE
}, {
    timestamps: false
});

or for all models:

var sequelize = new Sequelize('sequelize_test', 'root', null, {
    host: "127.0.0.1",
    dialect: 'mysql',
    define: {
        timestamps: false
    }
});

Solution 2 - node.js

I got the same error when migrating our project from laravel to featherjs. Tables are having column names created_at, updated_at instead of createdat, updatedat. I had to use field name mapping in Sequelize models as given below

  const users = sequelize.define('users', {
     id: {
         type: Sequelize.INTEGER,
         primaryKey: true
     },
     createdAt: {
         field: 'created_at',
         type: Sequelize.DATE,
     },
     updatedAt: {
         field: 'updated_at',
         type: Sequelize.DATE,
     },
     ..
     ..
     ..
     ..

Solution 3 - node.js

I got same error,two solutions:

  1. when create table, add created_at and updated_at column.
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
  `name` varchar(30) DEFAULT NULL COMMENT 'user name',
  `created_at` datetime DEFAULT NULL COMMENT 'created time',
  `updated_at` datetime DEFAULT NULL COMMENT 'updated time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='user';
  1. disable timestamps
const Project = sequelize.define('project', {
   title: Sequelize.STRING,
   description: Sequelize.TEXT
 },{
   timestamps: false
 })

Solution 4 - node.js

For postgresql:

const sequelize = new Sequelize('postgres://user:pass@url:port/dbname',{ 
    define:{
        timestamps: false
    }
})

Needless to say, replace user,pass,url,port and dbname values with your configuration values.

Solution 5 - node.js

In the general config this helped:

{
  timestamps: true,
}

After

{
  timestamps: true,
  underscored: true,
}

Solution 6 - node.js

Disable timestamps Ex:-

const User = sequelize.define('user', {
    firstName : Sequelize.STRING,
    lastName : Sequelize.STRING,
}, {
    timestamps: false
});

Solution 7 - node.js

well, maybe too late but you can create createdAt, updatedAt when migration like

      createdAt: {
        allowNull: false,
        defaultValue: Sequelize.fn('now'),
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        defaultValue: Sequelize.fn('now'),
        type: Sequelize.DATE
      }

Im using express and sequelize mysql then the model just define like normal for ex:

module.exports = (sequelize, DataTypes) => {
  const Customer = sequelize.define('customer', {
    name: DataTypes.STRING,
    email: DataTypes.STRING,
    phone: DataTypes.TEXT,
    consider: DataTypes.TEXT,
    otherQuestion: DataTypes.TEXT
  }, {})
  return Customer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid LimkysView Question on Stackoverflow
Solution 1 - node.jsJan Aagaard MeierView Answer on Stackoverflow
Solution 2 - node.jsPramodView Answer on Stackoverflow
Solution 3 - node.jszhuxyView Answer on Stackoverflow
Solution 4 - node.jsPravin DivraniyaView Answer on Stackoverflow
Solution 5 - node.jsPawelView Answer on Stackoverflow
Solution 6 - node.jsMaitraiya MaliView Answer on Stackoverflow
Solution 7 - node.jsMark SparrowView Answer on Stackoverflow