所以我可以为特定模型禁用时间戳列,但是有没有办法为所有模型禁用时间戳列?
const Contract = sequelize.define('Contract', {
idContract: {
type: Sequelize.INTEGER,
primaryKey: true
},
AccountNo_Lender: {
type: Sequelize.STRING
}
}, { timestamps: false });
答案 0 :(得分:4)
您可以在初始化sequelize
对象时定义它。
const sequelize = new Sequelize('test', 'postgres', 'postgres', {
host: '127.0.0.1',
dialect: 'postgres',
define: {
timestamps: false
},
});
define
下的选项与我们在模型中定义它们的方式相同。因此,模型中可以使用的所有选项也可以在这里使用。从docs
//指定在调用sequelize.define时使用的选项。
//以下示例://定义:{timestamps:false} //是 基本上与:// sequelize.define(name,attribute,{ timestamps:false})//因此定义每个模型的时间戳
答案 1 :(得分:2)
Sequelize构造函数带有一个define选项,它将更改所有已定义模型的默认选项。
const sequelize = new Sequelize(connectionURI, {
define: {
// The `timestamps` field specify whether or not the `createdAt` and `updatedAt` fields will be created.
// This was true by default, but now is false by default
timestamps: false
}
});
// Here `timestamps` will be false, so the `createdAt` and `updatedAt` fields will not be created.
class Foo extends Model {}
Foo.init({ /* ... */ }, { sequelize });
// Here `timestamps` is directly set to true, so the `createdAt` and `updatedAt` fields will be created.
class Bar extends Model {}
Bar.init({ /* ... */ }, { sequelize, timestamps: true });