Repository navigation
Description
What are you doing?
I was working on database ORM and migration recently, as we already have production database there, we need to create the initial migration for the database.
Environment:
Dialect: mysql
Dialect version: 5.7.20
Sequelize version: 4.7.2
My pain:
After comparing the database schema between the current product database and migration generate database. I found the foreign key constraint name is different. I do some further research and find the following code:
- when we run create database task, the sequelize will run the createTableQuery() function in /lib/dialects/query-generator.js and use the following code to define the foreign key constraint.
for (const fkey in foreignKeys) {
if (foreignKeys.hasOwnProperty(fkey)) {
values.attributes += ', FOREIGN KEY (' + this.quoteIdentifier(fkey) + ') ' + foreignKeys[fkey];
}
}
As this code hasn't define the constrain name, the constrain will name by innoDB, tablename_idfk_index
- when we run change column task, the sequelize will run the changeColumnQuery() function in the same file, however, this time the logic for naming foreign key constraint is different.
for (const attributeName in attributes) {
let definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) {
const fkName = this.quoteIdentifier(tableName + '_' + attributeName + '_foreign_idx');
const attrName = this.quoteIdentifier(attributeName);
definition = definition.replace(/.+?(?=REFERENCES)/, '');
constraintString.push(`${fkName} FOREIGN KEY (${attrName}) ${definition}`);
} else {
attrString.push('`' + attributeName + '` `' + attributeName + '` ' + definition);
}
}
By this time, the foreign key constraint name becomes tablename_foreignkeyName_foreign_idx
Then it leads the different naming conventions for foreign key constraint in the database. As I am going to remove the constraint when I need the update a foreign key attribute, I need to apply different logic to detect the constraint name. Is that any possibility to fix it? As letting InnoDB to naming foreign key constraint name make no sense to me. Thanks
Activity
sushantdhiman commentedon Oct 31, 2017
You are right these both version needs to be aligned, but that will be a breaking change I guess.
I would suggest you to run fine tuned alter queries to your migration. Change column for example have lots of internal checks as it has to track various type of column type which could change, an its buggy.
Another option will be combination of
johnny-dash commentedon Oct 31, 2017
Thanks, I am currently calling the removeConstraint() function in query-interface, that is basically the reason I need to query the constraint name as it is one of the parameters in removeConstraint(tableName, constraintName, option);
Anyway, It is fine for me to apply alter query now but it's nice to have this constraint name align in Sequelize tooling ; ]
DrTerr commentedon Jan 17, 2018
I have hit exactly same issue, while I can address it in my production environment, it is a huge problem, and makes half changeColumn function of queryInterface useless for constraints, as it will result in different name, making it harder to sync schema production and test database, as test database in my case is generated from scratch and production is migrated.
I don't think changing naming scheme to consistent naming should be considered breaking change, it is a major bug fix to inconsistent behavior.
hiradimir commentedon Apr 25, 2018
I have same issue. It is not safety to migrate with sequelize.