New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
{alter: true}
creates new index even if one already exists
#8984
Comments
…e indexes issue I added a new function in the query interface to remove all indexes in a table and called it in the sync function before making any changes in the table to eliminate any duplicates that would happen while altering fix sequelize#8984
…e indexes issue I added a new function in the query interface to remove all indexes in a table and called it in the sync function before making any changes in the table to eliminate any duplicates that would happen while altering fix sequelize#8984
Can you try with latest version. I added some tests, there were no duplicate indexes https://github.com/sequelize/sequelize/blob/master/test/integration/model/sync.test.js#L133-L209 |
I still have the issue with the latest version but I found the solution... I used to declare indexes like that const User = this.sequelize.define('testSync', {
email: {
type: Sequelize.STRING,
unique: true
},
}); But when I declare them like that I do not have the issue anymore. const User = this.sequelize.define('testSync', {
email: {
type: Sequelize.STRING
},
}, {
indexes: [
{ fields: ['email'], unique: true }
]
}); Thanks |
So problem is still there when unique: true is defined in attribute? odd, Will try adding more tests |
yes exactly ! |
Confirmed that this is still an issue in 4.37.6, using the solution from @adrienbarreau still resolves the issue. |
Still an issue in 5.21.11 |
I'm using version 6.3.5 and seeing exactly the same behaviour. |
Here is a workaround to delete new indices after calling Be careful, this will delete all indices of the current database that ends with a number after an underline character (e.g. index_1). const rawTables = await this.sequelize.query("SHOW TABLES");
const tables = rawTables[0].map(i => i[Object.keys(rawTables[0][0])[0]])
for (const t of tables){
const rawKeys = await this.sequelize.query(`SHOW INDEX FROM ${t}`);
const keys = rawKeys[0].map(i => i["Key_name"]).filter(i => i.match(/[a-zA-Z]+_\d+/))
for (const k of keys)
await this.sequelize.query(`ALTER TABLE ${t} DROP INDEX ${k}`)
} |
That's isn't the best way, but the fastest IMO.
This PR fixes issue sequelize#8984, about duplicating unique indexes while synchronizing structure of database. That's not the best solution, but easiest in my opinion.
{alter: true}
creates new index even if one already exists
This issue still exists for BadgeUser.init({
name: { type: DataTypes.STRING(32), allowNull: false },
description: { type: DataTypes.STRING(128), allowNull: false }
}, {
sequelize,
indexes: [
{ fields: ['name'], name: 'UQ_BadgeUser_Name', unique: true }
]
});
``` |
I'm currently experiencing this, on version 6.35.2 |
I confirm that the problem still persists! |
What are you doing?
I use {alter: true} in dev mode to update the model.
What do you expect to happen?
Update model and indexes
What is actually happening?
Every time the table is altered it adds new indexe.
For instance I now got 64 unique indexe on email. It should remove the indexe or at least do nothing if the constraint already exists.
Dialect: mysql
__Dialect version: mysql2 1.5.1
__Database version: Mysql 5.7
__Sequelize version: 4.32.2
The text was updated successfully, but these errors were encountered: