Skip to content

Commit

Permalink
fix(changeColumn): use engine defaults for foreign/unique key naming (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Sep 8, 2018
1 parent 27e4494 commit ebec0cf
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/dialects/mssql/query-generator.js
Expand Up @@ -260,8 +260,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
for (const attributeName in attributes) {
const definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) {
constraintString.push(_.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>', this._templateSettings)({
fkName: this.quoteIdentifier(attributeName + '_foreign_idx'),
constraintString.push(_.template('FOREIGN KEY (<%= attrName %>) <%= definition %>', this._templateSettings)({
attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/, '')
}));
Expand All @@ -279,7 +278,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
finalQuery += constraintString.length ? ' ' : '';
}
if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', ');
finalQuery += 'ADD ' + constraintString.join(', ');
}

return _.template(query, this._templateSettings)({
Expand Down
5 changes: 2 additions & 3 deletions lib/dialects/mysql/query-generator.js
Expand Up @@ -141,10 +141,9 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
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}`);
constraintString.push(`FOREIGN KEY (${attrName}) ${definition}`);
} else {
attrString.push('`' + attributeName + '` `' + attributeName + '` ' + definition);
}
Expand All @@ -156,7 +155,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
finalQuery += constraintString.length ? ' ' : '';
}
if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', ');
finalQuery += 'ADD ' + constraintString.join(', ');
}

return `ALTER TABLE ${this.quoteTable(tableName)} ${finalQuery};`;
Expand Down
4 changes: 2 additions & 2 deletions lib/dialects/postgres/query-generator.js
Expand Up @@ -294,15 +294,15 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {

attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({
tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
query: 'ADD UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
});
}

if (definition.match(/REFERENCES/)) {
definition = definition.replace(/.+?(?=REFERENCES)/, '');
attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({
tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition
query: 'ADD FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition
});
} else {
attrSql += _.template(query, this._templateSettings)({
Expand Down
2 changes: 1 addition & 1 deletion test/integration/query-interface/changeColumn.test.js
Expand Up @@ -113,7 +113,7 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});
}

//SQlite navitely doesnt support ALTER Foreign key
//SQlite natively doesn't support ALTER Foreign key
if (dialect !== 'sqlite') {
describe('should support foreign keys', () => {
beforeEach(function() {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/sql/change-column.test.js
Expand Up @@ -62,9 +62,9 @@ if (current.dialect.name !== 'sqlite') {
onDelete: 'cascade'
}).then(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 `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;'
mssql: 'ALTER TABLE [users] ADD FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;',
mysql: 'ALTER TABLE `users` ADD FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
postgres: 'ALTER TABLE "users" ADD FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;'
});
});
});
Expand Down

0 comments on commit ebec0cf

Please sign in to comment.