Sequelize table name change

不羁岁月 提交于 2019-12-21 17:01:47

问题


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.


回答1:


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
});



回答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



来源:https://stackoverflow.com/questions/49885921/sequelize-table-name-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!