30

I used sequelize-auto to generate schema, and I tried to use findOne() and I got this error:

Unhandled rejection SequelizeDatabaseError: Invalid column name 'updatedAt'.

in my database table, there is no field updatedAt.

for example, my table name is Users my code is Users.findOne(), and there is no updatedAt field in the Users table.

db.users= sequelize.import(__dirname + "/models/users");
app.get('/users', function (req, res) {

  db.user.findOne().then(function (project) {
    res.json(project);
  })
  
});

how to solve it?

Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
yozawiratama
  • 4,209
  • 12
  • 58
  • 106

2 Answers2

83

Refer to the documentation for configuration of Model in Sequelize

In model object of your table should look like this.

var user = sequelize.define('user', { /* bla */ }, {

  // don't add the timestamp attributes (updatedAt, createdAt)
  timestamps: false,

  // If don't want createdAt
  createdAt: false,

  // If don't want updatedAt
  updatedAt: false,

  // your other configuration here

});

Check Usage of sequelize-auto-generation module

  1. Create one json file for all model configuration options (flag object as defined here).

  2. When executing command you have to pass -a or --addtional option for that where you have to pass json file configuration path.

Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • oh ya, i get it too, so we must add config options in sequelize-auto generation, can you help me how to add the config? i just try it but still fail – yozawiratama Sep 20 '16 at 07:41
  • I have added some instructions in my answer – Haresh Vidja Sep 20 '16 at 07:54
  • 1
    The question seems to be about disabling only the updatedAt, not both columns. It shoudn't be the accepted answer, I believe. – Calhau Feb 24 '21 at 17:45
  • This approach should be the correct answer: https://stackoverflow.com/questions/45248189/not-store-updatedat-with-sequelize-model – Calhau Feb 24 '21 at 17:49
  • @RafaelCalhau Thanks for your attention.. I have changed my answer according to your concern. – Haresh Vidja Mar 22 '21 at 04:44
1

in Models => if you want to disable insert automatically createdAt, updatedAt use this

createdAt: { type: DataTypes.DATE, allowNull: true, defaultValue: sequelize.literal('CURRENT_TIMESTAMP') },
updatedAt: { type: DataTypes.DATE, allowNull: true, defaultValue: sequelize.literal('CURRENT_TIMESTAMP') }

in Services => if you want to to show only spesific column use this

async function getAll() {
    return await db.regencies.findAll({
        attributes: ['id', 'province_id', 'name']
    });
}
Jadi Akbar
  • 11
  • 1