How to force Sequelize to use unique table names

I have a model called User, but Sequelize is looking for the USERS table whenever I try to save to the database. Does anyone know how to set Sequelize to use unique table names? Thank you

+50
Jan 14 '14 at 13:15
source share
2 answers

In docs, you can use the freezeTableName property.

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 name tableName: 'my_very_custom_table_name' }) 
+98
Apr 20 '14 at 20:25
source share

While the accepted answer is correct, you can do this once for all tables, and not for this separately for each. You simply pass a similar object to the Sequelize constructor, for example:

 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 do not need to specify freezeTableName: true :

 var Project = sequelize.define('Project', { title: Sequelize.STRING, description: Sequelize.TEXT }) 
+43
Jan 01 '15 at 18:30
source share



All Articles