8

Have renamed a table from users to user in MySQL database. In Express I'm running Sequelize and created a schema for the old users table. With the table renamed and everything in the code changed from users to user, Sequelize is still looking for a table named specifically users even though the table name is updated in the schema.

User = db.sequelize.define('user', {
  first_name: Sequelize.STRING,
  last_name: Sequelize.STRING,
  email: Sequelize.STRING,
  password: Sequelize.STRING,
  role: Sequelize.STRING,
  created_at: Sequelize.DATE,
  updated_at: Sequelize.DATE
});

Tried running this to overwrite the table with one produced by Sequelize and it still created a table named users

User.sync({force: true}).then(() => {
  // Table created
  return User.create({
    firstName: 'test',
    lastName: 'test',
    email: 'test@example.com',
    password: 'password'
  });
});

Has anyone else experienced the same? Is there some other configuration I am missing? This is all I did in the first place when creating the first table.

anaximander
  • 357
  • 1
  • 5
  • 12
  • 1
    Does this answer your question? [How to make Sequelize use singular table names](https://stackoverflow.com/questions/21114499/how-to-make-sequelize-use-singular-table-names) – Rafael Tavares Apr 09 '20 at 20:49

3 Answers3

14

Sequelize pluralizes table name by default, Set freezeTableName: true to change this behavior like this.

User = db.sequelize.define('user', {
    first_name: Sequelize.STRING,
    last_name: Sequelize.STRING,
    email: Sequelize.STRING,
    password: Sequelize.STRING,
    role: Sequelize.STRING,
    created_at: Sequelize.DATE,
    updated_at: Sequelize.DATE
}, {
    freezeTableName: true
});
Mansoor Ul Haq
  • 320
  • 3
  • 8
  • Thanks for the answer. I am new to nodeJS and spent so much time to identify what was wrong with the sequelize. Thank you so much. – Mr. Noddy Oct 06 '18 at 15:44
2

Its been years since I've used the Sequelize framework, but I'm pretty sure the framework has a concept of pluralized table names by default. From memory, configuration needs to be provided in create to prevent this - so in conjunction with your non-pluralized table name, you'd also specify freezeTableName: true.

Looks like the docs state this:

http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
1

recent sequelize version allows you to do this

up: async (queryInterface, Sequelize) => queryInterface.showAllTables().then(async (tables) => {any function})