Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable timestamp columns globally for all models?

So I can disable the timestamp columns for a particular model but is there a way to disable it for all models?

const Contract = sequelize.define('Contract', {
    idContract: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    AccountNo_Lender: {
        type: Sequelize.STRING
    }
}, { timestamps: false });
like image 780
Rod Avatar asked Jun 21 '18 20:06

Rod


People also ask

How do I turn off timestamp?

You either have to declare public $timestamps = false; in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent. Just bare in mind pivot tables MUST have timestamps if you're using Eloquent.

How do I turn off timestamps in Sequelize?

If you don't want the columns, you need to set the timestamps option as false when you define your model: const User = sequelize. define( "User", { firstName: Sequelize. STRING }, { timestamps: false } );


1 Answers

You can define this when initializing the sequelize object.

const sequelize = new Sequelize('test', 'postgres', 'postgres', {
  host: '127.0.0.1',
  dialect: 'postgres',
  define: {
    timestamps: false
  },
});

Options under define are as same as when we define them inside the model. So all the options which can be used inside the model can be used here as well. From the docs

// Specify options, which are used when sequelize.define is called.
// The following example: // define: { timestamps: false } // is basically the same as: // sequelize.define(name, attributes, { timestamps: false }) // so defining the timestamps for each model will be not necessary

like image 121
AbhinavD Avatar answered Nov 15 '22 23:11

AbhinavD