How to make Sequelize use singular table names How to make Sequelize use singular table names node.js node.js

How to make Sequelize use singular table names


The docs state that you can use the property freezeTableName.

Please take a look at this example:

var Bar = sequelize.define('Bar', { /* bla */ }, {  // don't add the timestamp attributes (updatedAt, createdAt)  timestamps: false,  // don't delete database entries but set the newly added attribute deletedAt  // to the current date (when deletion was done). paranoid will only work if  // timestamps are enabled  paranoid: true,  // don't use camelcase for automatically added attributes but underscore style  // so updatedAt will be updated_at  underscored: true,  // disable the modification of tablenames; By default, sequelize will automatically  // transform all passed model names (first parameter of define) into plural.  // if you don't want that, set the following  freezeTableName: true,  // define the table's name  tableName: 'my_very_custom_table_name'})


While the accepted answer is correct, you can do this once for all tables rather than having to do it separately for each one. You simply pass in a similar options object into the Sequelize constructor, like so:

var Sequelize = require('sequelize');//database wide optionsvar opts = {    define: {        //prevent sequelize from pluralizing table names        freezeTableName: true    }}var sequelize = new Sequelize('mysql://root:123abc@localhost:3306/mydatabase', opts)

Now when you define your entities, you don't have to specify freezeTableName: true:

var Project = sequelize.define('Project', {    title: Sequelize.STRING,    description: Sequelize.TEXT})


You can Do it direct rather than specifying in every table you have define it oncelike below

var db_instance = new Sequelize(config.DB.database, config.DB.username, config.DB.password, {  host: config.DB.host,  dialect: config.DB.dialect,  define: {    timestamps: true,    freezeTableName: true  },  logging: false});  

OR

You can simply tell Sequelize the name of the table directly as well:

sequelize.define('User', {  // ... (attributes)}, {  tableName: 'Employees'});

You can see both Method in Documentation of sequelize.js

Doc. Of sequelize.js related to freezeTableName