Skip to content

Commit

Permalink
Include tableName in MySQL fk contraint names (#6008)
Browse files Browse the repository at this point in the history
When creating a foreign key constraint in MySQL, use the template
{referring_table}_{attribute}_foreign_idx, rather than just
{attribute}_foreign_idx, to avoid duplicate constraint names.

Related tests are updated.

See GitHub #5826
  • Loading branch information
hagabaka authored and janmeier committed Jun 2, 2016
1 parent 1f8cc7c commit 85477dd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/dialects/mysql/query-generator.js
Expand Up @@ -103,6 +103,7 @@ var QueryGenerator = {
key: this.quoteIdentifier(key),
definition: this.attributeToSQL(dataType, {
context: 'addColumn',
tableName: table,
foreignKey: key
})
});
Expand All @@ -129,7 +130,7 @@ var QueryGenerator = {
var definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) {
constraintString.push(Utils._.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>')({
fkName: this.quoteIdentifier(attributeName + '_foreign_idx'),
fkName: this.quoteIdentifier(tableName + '_' + attributeName + '_foreign_idx'),
attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/,'')
}));
Expand Down Expand Up @@ -270,7 +271,7 @@ var QueryGenerator = {
var attrName = options.foreignKey;

template += Utils._.template(', ADD CONSTRAINT <%= fkName %> FOREIGN KEY (<%= attrName %>)')({
fkName: this.quoteIdentifier(attrName + '_foreign_idx'),
fkName: this.quoteIdentifier(options.tableName + '_' + attrName + '_foreign_idx'),
attrName: this.quoteIdentifier(attrName)
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/sql/add-column.test.js
Expand Up @@ -39,7 +39,7 @@ if (current.dialect.name === 'mysql') {
onUpdate: 'cascade',
onDelete: 'cascade'
})), {
mysql: 'ALTER TABLE `users` ADD `level_id` INTEGER, ADD CONSTRAINT `level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
mysql: 'ALTER TABLE `users` ADD `level_id` INTEGER, ADD CONSTRAINT `users_level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/sql/change-column.test.js
Expand Up @@ -64,7 +64,7 @@ if (current.dialect.name !== 'sqlite') {
}).then(function(sql){
expectsql(sql, {
mssql: 'ALTER TABLE [users] ADD CONSTRAINT [level_id_foreign_idx] FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;',
mysql: 'ALTER TABLE `users` ADD CONSTRAINT `level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
mysql: 'ALTER TABLE `users` ADD CONSTRAINT `users_level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
postgres: 'ALTER TABLE "users" ADD CONSTRAINT "level_id_foreign_idx" FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;',
});
});
Expand Down

0 comments on commit 85477dd

Please sign in to comment.