yozawiratama
yozawiratama

Reputation: 4318

disable updatedAt (update date) field in sequelize.js

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?

Upvotes: 30

Views: 57593

Answers (2)

Jadi Akbar
Jadi Akbar

Reputation: 11

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']
    });
}

Upvotes: 1

Haresh Vidja
Haresh Vidja

Reputation: 8496

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.

Upvotes: 87

Related Questions