140

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

  • 4
    `user` is a reserved word, you'll have loads of problems if you really try to create a table with that name. –  Jan 14 '14 at 13:30
  • 2
    `user` is not a reserved word, but a keyword. While you won't encounter a problem using it, it's good to avoid it. https://dev.mysql.com/doc/refman/5.5/en/keywords.html – Nirmal Sep 30 '15 at 18:01

4 Answers4

286

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'
})
Felix
  • 124
  • 1
  • 4
  • 15
Luis Carlos Chavarría
  • 3,861
  • 2
  • 25
  • 26
  • 2
    I've updated the link in your question because it was broken, hope you don't mind! – Maria Ines Parnisari Aug 03 '16 at 17:26
  • 2
    `freezeTableName: true` is not working in the latest version of sequelize. Any other solution? – Muhammad Yasir Jul 29 '19 at 12:52
  • 3
    One downside to `freezeTableName` is that it _also_ prevents sqlz from lowercasing table and column names. Which means that later, when you're hand-writing SQL to dig through data, you have to cope with mixed-case names (whatever that means for your dialect). On pg, it means having to use double-quotes around every mixed-case name -- yuk! I wish we could opt-out of pluralization but retain case-folding... `define` has `tableName` option for explicit override. – Tom Nov 13 '19 at 05:33
  • 1
    use `freezeTableName: true` in addition of `modelName: 'singularName'` – Naor Levi Dec 02 '19 at 12:26
114

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 options
var 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
})
d512
  • 32,267
  • 28
  • 81
  • 107
  • 4
    Thanks for the heads-up, here is the [link to documentation](http://docs.sequelizejs.com/manual/installation/getting-started.html#application-wide-model-options) for those who interested. – ozanmuyes Apr 10 '18 at 03:46
14

You can Do it direct rather than specifying in every table you have define it once like 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

TheMineWay
  • 45
  • 6
Aryan
  • 3,338
  • 4
  • 18
  • 43
8

If you require to have different model names for singuar and plural definitions you can pass name as a parameter in options of model.

Please take a look at this example:

    const People = sequelize.define('people', {
    name: DataTypes.STRING,
}, {
    hooks: {
        beforeCount (options) {
            options.raw = true;
        }
    },
    tableName: 'people',
    name: {
        singular: 'person',
        plural: 'people'
    }
});

this will return "person" as an object when a single record is being queried and "people" as an array when we fetch multiple records.

Nidin Pereira
  • 96
  • 1
  • 3