不要怂,就是干,撸起袖子干!

Commit 85477ddc by Yaohan Chen Committed by Jan Aagaard Meier

Include tableName in MySQL fk contraint names (#6008)

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
1 parent 1f8cc7cc
...@@ -103,6 +103,7 @@ var QueryGenerator = { ...@@ -103,6 +103,7 @@ var QueryGenerator = {
key: this.quoteIdentifier(key), key: this.quoteIdentifier(key),
definition: this.attributeToSQL(dataType, { definition: this.attributeToSQL(dataType, {
context: 'addColumn', context: 'addColumn',
tableName: table,
foreignKey: key foreignKey: key
}) })
}); });
...@@ -129,7 +130,7 @@ var QueryGenerator = { ...@@ -129,7 +130,7 @@ var QueryGenerator = {
var definition = attributes[attributeName]; var definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) { if (definition.match(/REFERENCES/)) {
constraintString.push(Utils._.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>')({ 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), attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/,'') definition: definition.replace(/.+?(?=REFERENCES)/,'')
})); }));
...@@ -270,7 +271,7 @@ var QueryGenerator = { ...@@ -270,7 +271,7 @@ var QueryGenerator = {
var attrName = options.foreignKey; var attrName = options.foreignKey;
template += Utils._.template(', ADD CONSTRAINT <%= fkName %> FOREIGN KEY (<%= attrName %>)')({ 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) attrName: this.quoteIdentifier(attrName)
}); });
} }
......
...@@ -39,7 +39,7 @@ if (current.dialect.name === 'mysql') { ...@@ -39,7 +39,7 @@ if (current.dialect.name === 'mysql') {
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: '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;',
}); });
}); });
......
...@@ -64,7 +64,7 @@ if (current.dialect.name !== 'sqlite') { ...@@ -64,7 +64,7 @@ if (current.dialect.name !== 'sqlite') {
}).then(function(sql){ }).then(function(sql){
expectsql(sql, { expectsql(sql, {
mssql: 'ALTER TABLE [users] ADD CONSTRAINT [level_id_foreign_idx] FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;', 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;', postgres: 'ALTER TABLE "users" ADD CONSTRAINT "level_id_foreign_idx" FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;',
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!