Auto-generated Timestamps
By default, Sequelize automatically adds the attributes createdAt
and updatedAt
to every model, using the data type DataTypes.DATE
.
Those attributes are automatically managed as well - whenever you use Sequelize to create or update something, those attributes will be set correctly.
The createdAt
attribute will contain the timestamp representing the moment of creation, and the updatedAt
will contain the timestamp of the latest update.
Note: The value of these attributes are updated by Sequelize in JavaScript (i.e. not done with SQL triggers). This means that direct SQL queries (for example queries performed without Sequelize by any other means) will not cause these attributes to be updated automatically.
A third Auto-generated Timestamp called deletedAt
exists. It is used by Paranoid Models to mark a row as deleted.
TypeScript
If you're using TypeScript, you'll want to type these attributes. You can do so by simply adding them to your model as follows:
import { InferCreationAttributes, InferAttributes, Model, CreationOptional } from '@sequelize/core';
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
}
You don't need to decorate them, as they're automatically configured by Sequelize.
Disabling Auto-Timestamp Attributes
All auto-generated timestamp attributes can be removed from your model by setting the timestamps
option to false
:
@Table({ timestamps: false })
class User extends Model {}
The timestamps
option can also be disabled globally, on the Sequelize
instance:
class User extends Model {}
const sequelize = new Sequelize({
dialect: SqliteDialect,
define: {
timestamps: false,
},
models: [User],
});
It is also possible to disable only some auto-generated timestamp attributes
by setting the createdAt
or updatedAt
option to false:
@Table({
// don't generate a "createdAt" attribute
createdAt: false,
// don't generate an "updatedAt" attribute
updatedAt: false,
})
class User extends Model {}
Renaming Auto-Timestamp Attributes
You may also be interested in renaming the attributes. There are two parts to this:
- Renaming the JavaScript name (the attribute name)
- Renaming the Database name (the column name)
Changing the Attribute Name
Changing the attribute name
is done by using the @CreatedAt
, @UpdatedAt
, or @DeletedAt
decorators on the attribute of your choice.
- TypeScript
- JavaScript
import { InferCreationAttributes, InferAttributes, Model, CreationOptional } from '@sequelize/core';
import { CreatedAt, UpdatedAt, DeletedAt } from '@sequelize/core/decorators-legacy';
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
@CreatedAt
declare creationDate: CreationOptional<Date>;
@UpdatedAt
declare lastUpdateDate: CreationOptional<Date>;
@DeletedAt
declare deletionDate: Date | null;
}
import { Model } from '@sequelize/core';
import { CreatedAt, UpdatedAt, DeletedAt } from '@sequelize/core/decorators-legacy';
class User extends Model {
@CreatedAt
creationDate;
@UpdatedAt
lastUpdateDate;
@DeletedAt
deletionDate;
}
Using @DeletedAt
on your model will automatically enable Paranoid Mode for that model.
Changing the Column Name
Changing the Column Name is done by using the @ColumnName
decorator on the timestamp attribute.
If @ColumnName
is not used, the Column Name will be automatically inferred from the Attribute Name.
Read more on this in the Naming Strategies API documentation.
Notice how in the example below, we do not configure the data type or the nullability of these attributes. These properties will be configured automatically by Sequelize.
- TypeScript
- JavaScript
import { Model, InferCreationAttributes, InferAttributes, CreationOptional } from '@sequelize/core';
import { ColumnName } from '@sequelize/core/decorators-legacy';
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
// sets the Database name to 'created_at'
@ColumnName('created_at')
declare createdAt: CreationOptional<Date>;
// sets the Database name to 'updated_at'
@ColumnName('updated_at')
declare updatedAt: CreationOptional<Date>;
// for Paranoid Models
// sets the Database name to 'deleted_at'
@ColumnName('deleted_at')
declare deletedAt: Date | null;
}
import { Model } from '@sequelize/core';
import { ColumnName } from '@sequelize/core/decorators-legacy';
class User extends Model {
// sets the Database name to 'created_at'
@ColumnName('created_at')
createdAt;
// sets the Database name to 'updated_at'
@ColumnName('updated_at')
updatedAt;
// for Paranoid Models
// sets the Database name to 'deleted_at'
@ColumnName('deleted_at')
deletedAt;
}