Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(changeColumn): use engine defaults for foreign/unique key naming #9890

Merged
merged 1 commit into from Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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