Andrew R
Andrew R

Reputation: 161

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?

Upvotes: 13

Views: 16584

Answers (4)

Houda Abdellaoui
Houda Abdellaoui

Reputation: 1

I had the same issue, adding "underscored":true to the config file solved the problem.

    "development":
          {"host": "localhost",
           "dialect": "postgres",
           "quoteIdentifiers":false,
            "underscored":true }

Upvotes: 0

Safaa El khayari
Safaa El khayari

Reputation: 21

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'
},

Upvotes: 1

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.

Upvotes: 9

rajar
rajar

Reputation: 141

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);

Upvotes: 11

Related Questions