How to make Sequelize use singular table names
Set freezeTableName: true
in your Sequelize model definition to enforce the use of singular table names:
This property prevents automatic pluralization ensuring that Sequelize uses the exact model name as the table name.
Table naming: An overview
In Sequelize, table names are pluralized by default. For some projects, singular table names are preferred. Sequelize allows you to control this behavior.
Granular control over table naming
Customizing table names
To specify a custom table name, use the tableName
property:
This gives you the freedom to follow your naming conventions. Your Sequelize models can now reflect your business terminology or comply with existing database schemas.
Global setting for singular table names
Apply the freezeTableName: true
option within the Sequelize constructor for a global effect on all models:
This tactics ensures consistent singular table names across your entire database.
Adjusting automatic behaviors
Sequelize offers several options like timestamps
, underscored
, and paranoid
to further customize table behavior.
timestamps: false
prevents Sequelize from auto-generatingcreatedAt
andupdatedAt
fields.underscored: true
replaces camelCasing with underscore_lower_case for field names.paranoid: true
keeps deleted records by using adeletedAt
timestamp column.
Precise naming with 'name' parameter
For more granular control over how Sequelize refers to your model in associations, use the name
parameters with plural
and singular
values:
Tackling the Issue of Pluralization
Overriding automatic pluralization
To completely override default pluralization, apply the freezeTableName: true
option.
This approach offers a simple, consistent solution especially for new projects.
Applying global options to all models
Use define
property to set common options for all your models:
Implementing complex behaviors with hooks
Sequelize supports hooks for custom behaviors when working with models:
Whatever you need to do with your table data, there's probably a hook for that.
Was this article helpful?