Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the following error: "SequelizeDatabaseError column "createdAt" does not exist"

I've been going through this tutorial and the github repository for creating an express/passport/sequelize authorization using a Postgres database. I know what the issue is, I just need help with a solution.

After having everything in place and creating database, I try to login, but I keep getting the error:

"SequelizeDatabaseError" column "createdAt" does not exist".

There appears to be a bug where Sequelize 3.X.X is not able to read createdAt and the database I set up will automatically lowercase all tables created.

I believe there is a way to fix the createdAt requirement to be lowercase, but I can't seem to locate it at all. Has anyone else encountered a similar issue?

like image 959
Andrew R Avatar asked Oct 11 '16 00:10

Andrew R


3 Answers

if you dont want to take the time you should do:

var config = {
"define": {
    whatever: req.body.whatever,
    ...
  } ,{
    timestamps: false
}
}
with timestamps false you are not taking the time, that might be the problem.
like image 179
Antonio Miguel Toche Tuesta Avatar answered Nov 20 '22 06:11

Antonio Miguel Toche Tuesta


if you can't seem to fix the column name to createdAt then you can just add an extra field to the sequelize config

var config = {
"define": {
    "createdAt": "createdat",
    "updatedAt": "updatedat"
  } /*don't forget to add host, port, dialect, etc.*/
}
var sequelize = new Sequelize(database, username, password, config);
like image 43
rajar Avatar answered Nov 20 '22 08:11

rajar


I had the same issue and I fixed it by adding this to the sequelize config

const sequelize = new Sequelize({
....

define: {
underscored: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
}
});

create created-at, updated-at in migration like this :

  created_at: {
    type: Sequelize.DATE,
    allowNull: false,
    
   },
   updated_at: {
    allowNull: false,
    type: Sequelize.DATE,
   },

then make sure to add field name mapping in your models files :

 createdAt: {
  type: DataTypes.DATE,
  field: 'created_at'
 },

updatedAt: {
  type: DataTypes.DATE,
  field: 'updated_at'
},
like image 1
Safaa El khayari Avatar answered Nov 20 '22 07:11

Safaa El khayari