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

Commit 85412bf0 by Nathan Jones Committed by Jan Aagaard Meier

Backport #6008 (#7431)

Fix for MySQL fk constraint names to incorporate referring table name
and avoid possible name collisions.

Cherry-picked from 85477ddc
See also issue #5826
1 parent 0d1a558a
......@@ -103,6 +103,7 @@ var QueryGenerator = {
key: this.quoteIdentifier(key),
definition: this.attributeToSQL(dataType, {
context: 'addColumn',
tableName: table,
foreignKey: key
})
});
......@@ -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)/,'')
}));
......@@ -271,7 +272,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)
});
}
......
......@@ -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;',
});
});
......
......@@ -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;',
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!