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: '[email protected]',
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.
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
});
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
recent sequelize version allows you to do this
up: async (queryInterface, Sequelize) => queryInterface.showAllTables().then(async (tables) => {any function})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With